Is it possible to assume that Java enumerations automatically increase by 1?

Java claims that the sequence number of the initial value is 0. Is it possible to assume that when I create an enumeration as follows:

public enum Direction {MONDAY, TUESDAY, WEDNESDAY, THURSDAY, ...} 

That the sequence number TUESDAY always 1, then from WEDNESDAY always 2, ...?


I will be more specific. I declare the listing:

 public enum Direction {UP,RIGHT,DOWN,LEFT} 

Now there is a way to rotate 90 degrees (clockwise). This is one line with ordinals:

 direction = Direction.values()[direction.ordinal()+1 % Direction.values().length]; 

If I had not used ordinals, I would have to use switch statements or conditions:

 switch (direction) { case LEFT:newdirection = Direction.UP; break; etc... } 

There are several advantages to using ordinals:

  • shorter code
  • faster code (sloppy)
  • if a direction is added (e.g. DOWN_LEFT ), the implementation does not have to change if you put the new direction in the right place

What do you think?

+6
source share
6 answers

Yes - see javadoc :

Returns the ordinal number of this enumeration constant (its position in the enumeration declaration, where the initial constant is assigned a zero ordinal).

+7
source

Yes - but your code should not rely on it, because then it will break when someone inserts a katura.

+7
source

Yes, however, relying on this, your code becomes fragile. If you ever change the order of constants in an enumeration or add constants between them, the ordinals of some constants will change. Therefore, for example, it is not recommended to store serial numbers in a database or file, because if your source code changes, the data in the database or file will no longer be compatible with your modified code.

A more reliable solution is to explicitly store codes with each of your constants. For instance:

 public enum Status { NEW(0), PROCESSING(1), OK(2), ERROR(99); private final int code; private Status(int code) { this.code = code; } // Get the code for an enum constant public int getCode() { return code; } // Given a code, get the corresponding enum constant public static Status fromCode(int code) { for (Status s : Status.values()) { if (s.code == code) { return s; } } throw new IllegalArgumentException("Invalid code: " + code); } } 
+4
source

You can also apply indexes like this:

 enum Direction { MONDAY(1), TUESDAY(2); // etc... private int value; private Direction(int value) { this.value = value; } public int getValue() { return value; } } 
+3
source

Yes. However, relying on this is not a good idea if you ever want to reorder or add new listings in different places, or if you find a reason to change the existing value. I believe that it is better to give them real value / meaning, in addition to their ordinal value and name.

+2
source

The problem with ordinals is that they depend on the order in which enum values ​​are declared. If at some point a new value is added to the enumeration, and it is added in the middle of the others, then the ordinals will change, which will lead to the invalidity of the entire code, depending on a specific ordinal value.

If you absolutely need to assign a fixed number to an ordinal value, this can be useful:

 enum Directions { NORTH(1), SOUTH(2), WEST(3), EAST(4); private int code; Directions(int code) { this.code = code; } public int getCode() { return code; } } 

In the above snippet, the code attribute is associated with each enumeration value, and you can be sure that it will always have the same value - the one that was passed in the constructor.

+1
source

Source: https://habr.com/ru/post/910247/


All Articles