This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Should it not give compile time error? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Should it not give compile time error?" Watch "Should it not give compile time error?" New topic
Author

Should it not give compile time error?

indra negi
Ranch Hand

Joined: Aug 12, 2009
Posts: 68


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

Joined: Apr 23, 2009
Posts: 49
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

Joined: Jul 06, 2009
Posts: 196

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!


KARTHICK.C , SCJP6-93%
(Born to Win)
indra negi
Ranch Hand

Joined: Aug 12, 2009
Posts: 68
Thanks Thomas and Karthick. You are right. I got it.
karthick chinnathambi
Ranch Hand

Joined: Jul 06, 2009
Posts: 196

indra negi wrote:Thanks Thomas and Karthick. You are right. I got it.

you are welcome friend.......
Rashmi Liyan
Greenhorn

Joined: Aug 16, 2009
Posts: 9
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

Joined: Aug 03, 2008
Posts: 9291
    
  17

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...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Rashmi Liyan
Greenhorn

Joined: Aug 16, 2009
Posts: 9
Hi Ankit,
thanks . I got it.

/Rashmi
 
 
subject: Should it not give compile time error?
 
Similar Threads
Info concerning the updates to the SCJP 6 exam
Sun Ceritified Programmer for Java 6 Study Guide chapter-3 & question-7
Question on Enum
K & B, Chapter 3, Quiz Question 7
K&B Chapter 3 Question 7