Yes, you can use an instance of an enum as a field of another class. You can read about enums in the Java™ Tutorials.If you go through the Tutorials link, you will find that you can add price, calorific value, etc to the enum constants.
Carlos Davila wrote:Can an enum be considered an attribute of a class?
No, but - as others have said - you can certainly use an enum as an attribute of a class - in fact, it's often a very good thing to do.
And it doesn't matter where it's defined as long as the class can "see" it.
As Tim pointed out, an enum basically is a class, so it can also have attributes of its own. Indeed, it has two built in: it's number (ordinal()) and its name (name()); but you can add others just as for a regular class. you should remember though that each instance of an enum is a singleton - ie, only one of them can exist at any time - so you should generally make all attributes final.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
In the abstract sense, an enum is a subset of class integer with a rigidly-defined set of possible named values (in math terms, lts range. By default, they are continuous, but most languages that support enums also allow explicitly defining arbitrary values to the enum value names, providing that every value name maps to a unique value.
The value of enums is that A) instead of using "magic numbers" you use (hopefully) meaningful names in the same way you would with manifest constants. And B), you cannot actually assign arbitrary number values directly to a property (attribute) of type enum - it would cause a compilation error. You have to assign one of the value names or another enum of the same type.
Some languages assign a discrete namespace to the value names of an enum type so that EnumA can have a "valueX" and EnumB can also define a "valueX", but the 2 "valueX" names might not actually have the same corresponding integer value internally.
An IDE is no substitute for an Intelligent Developer.
Beware the other head of science - it bites! Nibble on this message: