Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Enum as an attribute?  RSS feed

 
Carlos Davila
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can an enum be considered an attribute of a class?

IE :
case1:an Enum originating inside a class
case2:Enum from some where else in the package?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66205
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is it specifically that you mean by attribute? Do you mean member?
 
Tim Holloway
Bartender
Posts: 18709
71
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Java, an enum is more like a class itself.

That's different from C/C++ where an enum is more like a typedef with value constraints.
 
Carlos Davila
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. for example. Can it be considered a attribute for instance
A Bank account is opened, closed
 
Campbell Ritchie
Marshal
Posts: 55717
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

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.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

HIH

Winston
 
Tim Holloway
Bartender
Posts: 18709
71
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!