Can I specify a sequence number for listing in Java?

The ordinal() method returns the sequence number of an enumeration instance.
How to set serial number for transfer?

+61
java enums
Mar 21 2018-11-11T00:
source share
7 answers

You cannot install it. It is always an order of constant determination. See Documentation for Enum.ordinal () :

Returns the sequence number of this enumeration constant (its position in the enumeration declaration, where the zero constant is assigned to the original constant). Most programmers will not use this method. It is intended for use by complex enum-based data structures such as EnumSet and EnumMap.

And in fact - you do not need. If you need some kind of integer property, define it.

+59
Mar 21 '11 at 1:13
source share

You can control the order by changing the order of the listing, but you cannot explicitly specify it in C++ . One way is to provide an additional method in your listing for the desired number:

 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 .

+75
Mar 21 '11 at 1:14
source share

You can update the serial number 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); } } 
+4
Sep 17 '14 at 14:25
source share

From http://download.oracle.com/javase/1.5.0/docs/api/java/lang/Enum.html

public final int orthinal () Returns the serial number of this enumeration constant (its position in the enumeration declaration, where the initial constant is assigned a zero ordinal). Most programmers will not use this method. It is intended for use by complex enum-based data structures such as EnumSet and EnumMap.

Returns: the ordinal of this enumeration constant

If you

public enum Day {SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY}

then SUNDAY has serial number 0, MONDAY is 1, and so on ...

+2
Mar 21 '11 at 1:16
source share

As the accepted answer says, you cannot set the sequence number. Closest to this is 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; } } 
+1
Oct 27 '17 at 9:19 on
source share

Check out Java Enum and docs

Returns the sequence number of this enumeration constant (its position in the enumeration declaration, where the zero constant is assigned to the original constant). Most programmers will not use this method. It is intended for use by complex enum-based data structures such as EnumSet and EnumMap.

0
Mar 21 '11 at 1:19
source share

Easy answer: just change the order of the constants. The first will be set to 0, the second will be equal to 1, etc. However, this may not be practical if you are constantly changing the code, or the enumerations will have many different values. You can define a custom method for working with the default sequence number, 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; } ... } } 
-2
Mar 21 '11 at 3:40
source share



All Articles