Can I specify ordinal for enum in Java?
JavaEnumsJava Problem Overview
The ordinal()
method returns the ordinal of an enum instance.
How can I set the ordinal for an enum?
Java Solutions
Solution 1 - Java
You can control the ordinal by changing the order of the enum, but you cannot set it explicitly like in C++
. One workaround is to provide an extra method in your enum for the number you want:
enum Foo {
BAR(3),
BAZ(5);
private final int val;
private Foo(int v) { val = v; }
public int getVal() { return val; }
}
In this situation BAR.ordinal() == 0
, but BAR.getVal() == 3
.
Solution 2 - Java
You can't set it. It is always the ordinal of the constant definition. See the documentation for Enum.ordinal():
> 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. It is designed for use by sophisticated enum-based data structures, such as EnumSet and EnumMap.
And actually - you should not need to. If you want some integer property, define one.
Solution 3 - Java
As the accepted answer points out, you can't set the ordinal. The closest you can get to this is with a custom property:
public enum MonthEnum {
JANUARY(1),
FEBRUARY(2),
MARCH(3),
APRIL(4),
MAY(5),
JUNE(6),
JULY(7),
AUGUST(8),
SEPTEMBER(9),
OCTOBER(10),
NOVEMBER(11),
DECEMBER(12);
MonthEnum(int monthOfYear) {
this.monthOfYear = monthOfYear;
}
private int monthOfYear;
public int asMonthOfYear() {
return monthOfYear;
}
}
Note: By default, enum
values start at 0
(not 1
) if you don't specify values. Also, the values do not have to increment by 1
for each item.
Solution 4 - Java
You can update ordinal using reflection:
private void setEnumOrdinal(Enum object, int ordinal) {
Field field;
try {
field = object.getClass().getSuperclass().getDeclaredField("ordinal");
field.setAccessible(true);
field.set(object, ordinal);
} catch (Exception ex) {
throw new RuntimeException("Can't update enum ordinal: " + ex);
}
}
Solution 5 - Java
From http://download.oracle.com/javase/1.5.0/docs/api/java/lang/Enum.html
> public final int ordinal()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. It is designed for use by sophisticated enum-based data structures, such as EnumSet and EnumMap.
>Returns: the ordinal of this enumeration constant
If you have
> public enum Day { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY }
then SUNDAY has an ordinal of 0, MONDAY is 1, and so on...
Solution 6 - Java
Check out the Java Enum examples and docs
> 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. It is designed for use by sophisticated enum-based data structures, such as EnumSet and EnumMap.
Solution 7 - Java
The easy answer: just change the order of the constants. The first defined will be 0, the second will be 1, etc. However, this may not be practical if you have constantly changing code, or enums will many many values. You can define a custom method to work around the default ordinal, but MAKE SURE it is well documented to avoid confusion!
public enum Values
{
ONE, TWO, THREE, FOUR;
public int getCustomOrdinal()
{
if(this == ONE)
{
return 3;
}
else if(this == TWO)
{
return 0;
}
...
}
}