| 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.
|
 |
 |
|
|
subject: Enum Constant Specific Class Body + private member
|
|
|