0

This should be total obvious, but it isn't. Is there a way to get the value of a enum as in the example below?

example:

    public enum Numbers
    {
        zero, one, two

    }

//later on
Numbers numbers = Numbers.zero;
// this is the part that I can not figure out the java equivalent of?
int number = numbers.value();

Does this make sense what I am trying to do?

WIllJBD
  • 6,144
  • 3
  • 34
  • 44
  • 3
    http://docs.oracle.com/javase/7/docs/api/java/lang/Enum.html#ordinal() - that said, it's not useful information. Why do you need to know? – Brian Roach May 22 '13 at 17:47
  • ^^ "Returns the ordinal of this enumeration constant (its position in its enum declaration, where the initial constant is assigned an ordinal of zero). Most programmers will have no use for this method." I saw this in the documentation but it did not look like what I was needing... Is it though? – WIllJBD May 22 '13 at 17:49
  • Well, since there's really no such thing as a "value" of an enum I don't know what it is you need. That'd be the closest thing to it as it's an integer that refers to the ordering. – Brian Roach May 22 '13 at 17:52
  • I have an enum which keeps track of a state machine running on a thread, and I needed the value of the enum as a integer to integrate into another's part of it who used 30 something constant int's instead of a enum. :( – WIllJBD May 22 '13 at 17:54
  • I would suggest checking out the Oracle tutorial on `enum` - you would want to define your own scheme for this that you pass in and provide a getter rather than relying on the ordinal. – Brian Roach May 22 '13 at 17:55
  • Have you tried `valueOf`? –  May 22 '13 at 17:56
  • In an ideal world you would update the other part to use the enums instead of constants. In the real world use ordinal() very carefully, or, define a method `int state()` that returns the int for the state. – user949300 May 22 '13 at 17:56
  • possible duplicate of [How to convert enum value to int?](http://stackoverflow.com/questions/8157755/how-to-convert-enum-value-to-int) – rgettman May 22 '13 at 18:27

4 Answers4

5

It is generally considered bad practice to rely on ordinal since it is based on the order of the enum constants. A better solution would be to pass information to each instance. Just write a constructor and method like this:

public enum Number {
    ZERO(0), ONE(1), TWO(2);

    private int value;

    private Number(int value) {
        this.value = value;
    }

    public int value() {
        return value;
    }
};

Then you use it like you want

Number numbers = Number.ZERO;
int number = numbers.value();

and you won't have to worry about the actual order of the code if you add Numbers later.

Note: It is idiomatic in Java for enum names to be singular and for constants to be uppercase.

Matt Eckert
  • 1,946
  • 14
  • 16
3

Based on your comments it appears you need to map an integer value to your enum to integrate it into another part of your code.

Rather than relying on the ordering you really want to do this yourself:

public enum MyEnum {
    ZERO(0), ONE(1), TWO(2);

    private int mapping;

    MyEnum(int mapping) {
        this.mapping = mapping;
    }

    public int getMapping() {
        return mapping;
    }

} 

You could then compare via:

MyEnum e = MyEnum.ZERO;
if (someInt == e.getMapping()) { ... } 

(Or even just MyEnum.ZERO.getMapping() )

Brian Roach
  • 76,169
  • 12
  • 136
  • 161
  • I up voted everyone since they are all valid answers, however you were the first to start helping and I thank you for your help, so I accepted yours. – WIllJBD May 22 '13 at 18:03
2

You can use the ordinal() method. However, this is discouraged / risky, since if you ever change the order or arrangements of your enums, it will fail. e.g., you change to powers of two

public enum Numbers {
   zero, one, two, four
}

then four.ordinal() will be 3.

If you really want a number associated with the enum, you should define your own method, e.g. order() or whatever. In the initial implementation it could return ordinal(), but, when you add four in the future, you can update that method.

p.s. Standard style is to capitalize the enums, e.g. ZERO, ONE, etc...

user949300
  • 15,364
  • 7
  • 35
  • 66
  • Thank you, this is what i needed. The actual emun I am using is not zero, one, two, three, it is 30something differing states, and for my portion of it at least, this enum's ordering will never be changed. – WIllJBD May 22 '13 at 17:56
  • Ideally I'd still define a method `int state()`, but using ordinal is tolerable. – user949300 May 22 '13 at 17:57
1

It depends on what you need. Here are 2 use cases:

1) You will probably need to test the value of an incoming "Numbers". For this use case, a switch case is the best solution.

void myMethod(Numbers n){

  switch(n){
    case zero:
    //do something
    break;

    case one:
    //do something
    break;

    //etc.
  }
}

2) You can calso need to assign a value to each Numbers and be able to retrieve it. For this use case, you can define a constructor for your enum

public enum Numbers
{
    zero(0), one(1), two(2);

    private int value;

    private Numbers(int value){
      this.value=value;
    }

}

Then, in your code, you can get this value:

void myMethod(Numbers n){
  int value = n.getValue();
  //do something with this value...
}

Last remark : Your enum names don't follow the java convention. They should be named with uppercase letters and eventually character '_'. ex: ONE, TWO, THREE, ANOTHER_VALUE

Arnaud Denoyelle
  • 29,980
  • 16
  • 92
  • 148