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

Sierra/Bates SCJP 6 - enum

 
Mark Buckler
Greenhorn
Posts: 6
Chrome Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 394
Eclipse IDE Java Oracle
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mark Buckler
Greenhorn
Posts: 6
Chrome Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mark,

Enums declared within classes are implicitly static. So instantiation would happen when the class loads.
 
Mark Buckler
Greenhorn
Posts: 6
Chrome Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 394
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 6
Chrome Java Windows XP
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic