Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Should it not give compile time error?

 
indra negi
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


In the above code points has been declared as of private type and it is being accessed from the main method with the following syntax:
System.out.println(Suits.SPADES+" "+Suits.SPADES.points);

Should it not give compile time error at line no. 20.
 
Tommi Vd
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No it shouldn't, the enum is declared in the same class as where the main method is.
If you put the enum outside the class, you will get a compile time error that points isn't accessible.
 
karthick chinnathambi
Ranch Hand
Posts: 196
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
indra negi wrote:
System.out.println(Suits.SPADES+" "+Suits.SPADES.points);
Should it not give compile time error at line no. 20.



here the main() method is inside the same class in which you have declared the enum....
i suppose that you already knew that its perfectly legal to access a private member inside the class in which it is declared....

adding to it you should know that all enums are almost like classes which implicitly extend java.lang.Enum and that their constructors can't be invoked explicitly.....

i suppose that you will understand this clearly...
Thank you!
 
indra negi
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Thomas and Karthick. You are right. I got it.
 
karthick chinnathambi
Ranch Hand
Posts: 196
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
indra negi wrote:Thanks Thomas and Karthick. You are right. I got it.

you are welcome friend.......
 
Rashmi Liyan
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Can someone explain the out put given by the

System.out.println(Suits.values());

it gives '...Bridge$Suits;@1372a1a' as output.

What's this ?

/Rashmi.
 
Ankit Garg
Sheriff
Posts: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well this is the default value returned by the toString method. You don't need to understand @1372a1a came from. Before it is the enum name (Bridge is the containing class of Suits enum so Bridge$Suits) and after that is a hash code. If a question like this comes, you just have to know that there is a weird looking value that the default toString method returns...
 
Rashmi Liyan
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ankit,
thanks . I got it.

/Rashmi
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic