This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Enum Constant Specific Class Body + private member Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Enum Constant Specific Class Body + private member" Watch "Enum Constant Specific Class Body + private member" New topic
Author

Enum Constant Specific Class Body + private member

Adolfo Eloy
Ranch Hand

Joined: Mar 21, 2009
Posts: 142

Hello all.

I'm not understanding the code below. Why the instance variable ounces for the CoffeSize enum isn't visible within the constant specific class body getOunces since this all belongs to the same enum definition?

Excerpt from SCJP Kathy Sierra & Bert Bates - Chapter 1

EDIT: changed to quote the code with reference book.


Thanks


Adolfo Eloy
Software Developer
OCPJP 6
Tom Reilly
Rancher

Joined: Jun 01, 2010
Posts: 618
Interesting question. Note that I have no expertise in this but I found the following paragraph at the Java Community Process:
Java Community Process wrote:The use of class modifiers in enum declarations is as for class declarations, with a few additional restrictions. All enum declarations are implicitly final unless they contain constant-specific class bodies (which result in implicit subclasses). It is permissible to use the final modifier on an enum declaration without constant-specific class bodies, but it has no effect (and is discouraged). Enum declarations may not use the class modifier abstract unless they contain constant-specific class bodies for every enum constant, and any abstract methods declared in the (optional) class body declarations are overridden in all the constant-specific class bodies. Enum member classes are implicitly static. Regardless of what class modifiers are used, it is illegal to explicitly instantiate an enum (using new).
Note the "(which result in implicit subclasses)". So I surmise the ounces variable cannot be private because the constant-specific class body is, in effect, a subclass of the enum and so it cannot access private members of its super class.

BTW, you should QuoteYourSources.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Enum Constant Specific Class Body + private member
 
Similar Threads
How to call method which was in enum class
confusion regardind enum and constructors
Hard Exam question