aspose file tools*
The moose likes Beginning Java and the fly likes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark ""casting" an int to an enum type" Watch ""casting" an int to an enum type" New topic
Author

"casting" an int to an enum type

Kurt Van Etten
Ranch Hand

Joined: Sep 07, 2010
Posts: 98
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

Joined: Apr 06, 2010
Posts: 4387
    
    8

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

Joined: Sep 29, 2008
Posts: 12617

Or create a map in the enum, because enum ordinals need not be sequential.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19693
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3646
    
  16

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

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Jan 28, 2008
Posts: 5575

Welcome to JavaRanch Kurt
Kurt Van Etten
Ranch Hand

Joined: Sep 07, 2010
Posts: 98
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

Joined: Sep 20, 2010
Posts: 3646
    
  16

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: "casting" an int to an enum type