Enum with int value in Java

34,030

Solution 1

If you want attributes for your enum you need to define it like this:

public enum Foo {
    BAR (0),
    BAZ (1),
    FII (10);

    private final int index;   

    Foo(int index) {
        this.index = index;
    }

    public int index() { 
        return index; 
    }

}

You'd use it like this:

public static void main(String[] args) {
    for (Foo f : Foo.values()) {
       System.out.printf("%s has index %d%n", f, f.index());
    }
}

The thing to realise is that enum is just a shortcut for creating a class, so you can add whatever attributes and methods you want to the class.

If you don't want to define any methods on your enum you could change the scope of the member variables and make them public, but that's not what they do in the example on the Sun website.

Solution 2

If you have a contiguous range of values, and all you need is the integer value, you can just declare the enum minimally:

public enum NUMBERZ {
        ZERO, ONE, TWO
}

and then obtain the int value as follows:

int numberOne = NUMBERZ.ONE.ordinal();

However, if you need a discontiguous range (as in your example, where you jump from 1 to 10) then you will need to write your own enum constructor which sets your own member variable, and provide a get method for that variable, as described in the other answers here.

Solution 3

It is:

enum Foo
{
  Bar(0),
  Baz(1),
  Fii(10);

  private int index;

  private Foo(int index) {
      this.index = index;
  }
}

Note that to get the value of the enum from the index, Foo.valueOf(1) (*), would not work. You need do code it yourself:

public Foo getFooFromIndex(int index) {
    switch (index) {
    case 0:
        return Foo.Bar;
    case 1:
        return Foo.Baz;
    case 10:
        return Foo.Fii;

    default:
        throw new RuntimeException("Unknown index:" + index);
    }
}

(*): Enum.valueOf() return the enum from a String. As such, you can get the value Bar with Foo.valueOf('Bar')

Solution 4

Sounds like you want something like this:

public enum Foo {
    Bar(0),
    Baz(1),
    Fii(10);

    private int number;

    public Foo(int number) {
       this.number = number;
    }

    public int getNumber() {
        return number;
    }
}

For starters, Sun's Java Enum Tutorial would be a great place to learn more.

Share:
34,030
ripper234
Author by

ripper234

See blog or LinkedIn Profile

Updated on July 09, 2022

Comments

  • ripper234
    ripper234 almost 2 years

    What's the Java equivalent of C#'s:

    enum Foo
    {
      Bar = 0,
      Baz = 1,
      Fii = 10,
    }
    
  • Matt H
    Matt H over 14 years
    +1 for including the accessor :) Note that to get to the numerical value, you'd have to use Foo.BAR.index()
  • penpen
    penpen over 14 years
    Note also, that if you want to get to the enum from the int code, you shoudl also add a static method that do the reverse mapping (with a switch or a map).
  • Kevin Bourrillion
    Kevin Bourrillion over 14 years
    Instead of that hand-coded method, lazily initialize a Map<Foo,Integer>. (for example using Maps.uniqueIndex() from Google Collections.)
  • Kevin Bourrillion
    Kevin Bourrillion over 14 years
    @penpen - a switch is a bummer to maintain; do the map, or just do a linear search, it's not that bad (except when it is)
  • Soundlink
    Soundlink over 13 years
    Keep in mind, that the constructor for an enum type must be package-private or private access, not public.
  • Robert
    Robert over 12 years
    Too bad it's not possible to use Bar.index, Baz.index etc in the switch because they aren't constant expressions.
  • epeleg
    epeleg over 11 years
    I would not call this index but val instead as for example the index (ordinal) of FII in this code is 2 (assuming zero based indexing) while its value is 10.