This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Java in General and the fly likes Storing values in enum Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Storing values in enum" Watch "Storing values in enum" New topic
Author

Storing values in enum

Jan Hoppmann
Ranch Hand

Joined: Jul 19, 2010
Posts: 147

Hi!

My problem is the following: I have a class with a (not static) enum in it that represents 6 values. If I now create an instance of this class, are the values in the enum independent from the values in other objects?


Life is full of choices. Sometimes you make the good ones, and sometimes you have to kill all the witnesses.
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
So I gather that your enum is defined as an inner class? If so, by definition per the JLS, inner Enum classes are implicitly static inner classes and so the enum's values are the same everywhere. In recognition of their being a static inner class, you need to access the enum from the outer class name and not an instance variable name:

Per the JLS, section 8.9:

Nested enum types are implicitly static. It is permissable to explicitly declare a nested enum type to be static.
Jan Hoppmann
Ranch Hand

Joined: Jul 19, 2010
Posts: 147

Yes, that's exactly what I tried to do. Okay, so I'll change my implementation to a Map instead.
Enums are still new (and quite confusing) to me
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Storing values in enum