aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Sierra/Bates SCJP 6 - enum Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Sierra/Bates SCJP 6 - enum" Watch "Sierra/Bates SCJP 6 - enum" New topic
Author

Sierra/Bates SCJP 6 - enum

Mark Buckler
Greenhorn

Joined: May 23, 2011
Posts: 6

Regarding Self Test Answers Chapter 3 Page 281 Question 7. You have probably heard many times that the code won't compile due to the reference to Suits.NOTRUMP.getBidValue(3) on Line 12. Not my real question, no big deal, but in case you didn't know about it there it is.

My real question is "private int points" in the same code. It is stated that this is a private instance variable, back in explanation of enum declarations on Page 63. If that is the case, why would line 13 compile when it sees Suits.SPADES.points? I am going to take a stab at this and say that the enum declaration is treated as an inner class declaration and instantiated for each enum value. However, that must be accomplished "within" main, because main is static. Please clarify, thank you.
Ikpefua Jacob-Obinyan
Ranch Hand

Joined: Aug 31, 2010
Posts: 394

Mark Buckler wrote:
I am going to take a stab at this and say that the enum declaration is treated as an inner class declaration and instantiated for each enum value. However, that must be accomplished "within" main, because main is static.


Hello Mark,
welcome to Javaranch.
I simply want to inform you that in java it is ILLEGAL to declare enums in methods, independently of the methods context.

regards

Ikpefua.


OCPJP 6.
In Your Pursuit Towards Certification, NEVER Give Up.
Mark Buckler
Greenhorn

Joined: May 23, 2011
Posts: 6

Thank you, I am totally aware of that and it is not the issue because it is not declared within a method. It is declared in the same class in which main is declared as a method and it is declared outside any methods, so it is legal. However, my question is whether the enum is implicitly instantiated within the main method, as it seems to me that would be the only way to reach the private instance variable I am referring to. You will need to look at the code on page 281 Self Test Answers.
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
Hi Mark,

Enums declared within classes are implicitly static. So instantiation would happen when the class loads.
Mark Buckler
Greenhorn

Joined: May 23, 2011
Posts: 6

Thank you, that still does not answer how the "private instance variable" is accessed by a line of code in main unless it is treated as an inner class that is instantiated within main. Take a look at the code and see the reference to Suits.SPADES.points. That is the private instance variable. Do you have a copy of the book and the code I am referring to?
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
Mark Buckler wrote:Thank you, that still does not answer how the "private instance variable" is accessed by a line of code in main unless it is treated as an inner class that is instantiated within main. Take a look at the code and see the reference to Suits.SPADES.points. That is the private instance variable. Do you have a copy of the book and the code I am referring to?


The elements of the Suits enum have been instantiated well before the main method executes.
Ikpefua Jacob-Obinyan
Ranch Hand

Joined: Aug 31, 2010
Posts: 394


Mark I guess this is your doubt...Line thirteen compiles because the enum is declared within the class, hence it is a MEMBER of the class, and private members can be acessed within the SAME class.

Lets take a look at this SAME situation with the enum decalred outside the class


To be sincere with you, I am impressed with your intelligent observation...keep it up.
One more thing...I advice you to post your codes here, lets PHYSICALLY see what we are arguing, it saves us precious time.
Mark Buckler
Greenhorn

Joined: May 23, 2011
Posts: 6

Thanks so much. I will post the code as well next time. This does confirm what I was thinking, that the enum is treated as an inner class so the private instance variable can be accessed in main. Regards.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Sierra/Bates SCJP 6 - enum