integer value of enum constant??

Q

qazmlp1209

I need to create an Integer class with the integer value of the enum
constants. I'm not sure which enum interface I can use for this
purpose.
Should I use the following one, or is there a different way of getting
it?
"valueOf(Class<T> enumType, String name)"
 
T

tom fredriksen

I need to create an Integer class with the integer value of the enum
constants. I'm not sure which enum interface I can use for this
purpose.
Should I use the following one, or is there a different way of getting
it?
"valueOf(Class<T> enumType, String name)"

Search the group, there are numerous examples of doing similar thing
already.

/tom
 
T

tom fredriksen

Roedy said:
There is nothing to do. Java generates you the method automatically.

How do you mean? AFAICT, what he is looking for is a mapping between a
enum name ONE and the integer value 1, the best way to do that is by
with enum constructor and an enum method

int getValue()

Are you saying that java generates such a method automatically, without
it knowing how you want the mapping?

/tom
 
R

Roedy Green

How do you mean? AFAICT, what he is looking for is a mapping between a
enum name ONE and the integer value 1, the best way to do that is by
with enum constructor and an enum method

int getValue()

Are you saying that java generates such a method automatically, without
it knowing how you want the mapping?

yes. Java assigns ordinals in the order you defined your enum
constants without gaps. You are of course free to create additional
more C-like mappings, e.g to ints, letters, short strings, long
strings etc.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top