This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Enums with numbers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Enums with numbers" Watch "Enums with numbers" New topic

Enums with numbers

Pat Short

Joined: Mar 21, 2008
Posts: 22

how do I define an enum of number & non alpha characters. For e.g. if I want to define an enum of 1,2 & 3 I thought I could do the following


I know that I could define strings with these values as follows

but that does seem right either. What are the rules about what an enum value can have?

Steve Luke

Joined: Jan 28, 2003
Posts: 4165

Like all Java identifiers, enum names must be alpha-numeric starting with an alphabetic character.

marc weber

Joined: Aug 31, 2004
Posts: 11343

As Steve said above, enum identifiers follow the same rules as other Java identifiers. By convention, enum identifiers are typically all uppercase.

But I'm not sure if that's what you're asking.

Note that each Enum instance has an int ordinal that represents the Enum's "position in its enum declaration, where the initial constant is assigned an ordinal of zero." This value is returned by the method ordinal(). For details, see the API documentation for java.lang.Enum.

Additionally, you can add data and behavior to your Enums. See this Enums article for details (note the Planet example about half way through).

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: Enums with numbers
Similar Threads
"casting" an int to an enum type
Enums (Is this the best way)?
enums do not allow access to static members within constructors and instance init blocks
Hard Exam question