Enum with int value in Java

C#JavaEnums

C# Problem Overview


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

enum Foo
{
  Bar = 0,
  Baz = 1,
  Fii = 10,
}

C# Solutions


Solution 1 - C#

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 - C#

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 - C#

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 - C#

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.

Solution 5 - C#

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

    private final int someint;
    Foo(int someint) {
        this.someint = someint;
    }
}

In Java enums are very similar to other classes but the the Java compiler knows to treat a little differently in various situations. So if you want data in them like you seem to you need to have an instance variable for the data and an appropriate constructor.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionripper234View Question on Stackoverflow
Solution 1 - C#Dave WebbView Answer on Stackoverflow
Solution 2 - C#CarlView Answer on Stackoverflow
Solution 3 - C#Thierry-Dimitri RoyView Answer on Stackoverflow
Solution 4 - C#PeterView Answer on Stackoverflow
Solution 5 - C#Tendayi MawusheView Answer on Stackoverflow