Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Enums with numbers

 
Pat Short
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

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


or


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?

Thanks
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Like all Java identifiers, enum names must be alpha-numeric starting with an alphabetic character.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic