List order
Yes. The Java Language Specification for Enums states:
/**
* Returns an array containing the constants of this enum
* type, in the order they're declared. This method may be
* used to iterate over the constants as follows:
*
* for(E c : E.values())
* System.out.println(c);
*
* @return an array containing the constants of this enum
* type, in the order they're declared
*/
public static E[] values();
It will return an array with constants as they are declared.
As for the method Arrays.asList()
, you can also rely on its order:
Returns a fixed-size list supported by the specified array. (Changes to the returned "write through" list to the array.)
Consider the example below, which is a very common way to initialize List
:
List<String> stooges = Arrays.asList("Larry", "Moe", "Curly");
The order of the list will be the same as in the array.
source to share
The JLS mentions that values()
"Returns an array containing the constants of this enumeration type, in the order in which they are declared." ( http://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.9 ). So, you can assume that the order will be the same until your enum type changes
For some details see How are values () implemented for Java 6 enums?
source to share