• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

"casting" an int to an enum type

 
Kurt Van Etten
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Enum types in Java provide an ordinal() method to supply the int value associated with a particular enum value. I'd like to go in the other direction, "casting" an int value to a corresponding enum. Of course, Java won't let me do an actual cast. The only suggestion I've seen on how to accomplish this is by setting up an array of the enum values, so that the array index equals the enum ordinal. This seems very kludgy, though. Is there a better way of handling this?
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you've got an enum MyEnum, then MyEnum.values() returns an array of the values in the order they're declared. So you can just use that - no need to set up an additional array.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or create a map in the enum, because enum ordinals need not be sequential.
 
Rob Spoor
Sheriff
Pie
Posts: 20659
64
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know about that. The Javadoc of ordinal() says
Returns the ordinal of this enumeration constant (its position in its enum declaration, where the initial constant is assigned an ordinal of zero)
Since the ordinal of the first constant is said to be 0 and the ordinal is the position, I see that as a guarantee that the ordinal() values will be sequential and you can in fact use them as array indexes.
 
Stephan van Hulst
Bartender
Posts: 6310
77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Be careful with "casting" ints to Enum types, and vice versa. You can use ordinal() to work out the ordering of an Enum type, but you should never use it to convert between the two.
The reverse is true as well. You shouldn't derive an Enum from an integer value, unless you associate this value with the Enum; for instance with a constructor:



This way you can get an Enum by calling MyEnum.fromValue(int) and you can convert back to the value with someMyEnum.value().
This seems like a roundabout way to do it, but it means that if you later insert another MyEnum instance in your list, your values will remain the same, even if the ordinal() value changes.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:I don't know about that. The Javadoc of ordinal() says
Returns the ordinal of this enumeration constant (its position in its enum declaration, where the initial constant is assigned an ordinal of zero)
Since the ordinal of the first constant is said to be 0 and the ordinal is the position, I see that as a guarantee that the ordinal() values will be sequential and you can in fact use them as array indexes.

Oh, right, I was thinking about constructors, and completely confusing Java with other languages--never mind.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch Kurt
 
Kurt Van Etten
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Everyone, thank you for the replies--it was a big help. The .values() method will do exactly what I need for my current task. Stephan, I had to read your code several times before it sank in; I had no idea you could do that kind of thing with enums. I'll have to remember that for the future.
 
Stephan van Hulst
Bartender
Posts: 6310
77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yup, it's a very nifty feature. Remember that the constructor always has to be private though. You can't create Enum instances outside of their class.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic