I was looking through the documentation and source code, because I wanted to be certain that values() would always return an array in the order in which the Enum values are declared. Turns out, it’s not in the documentation as far as I can tell.
I checked the source code for the Enum class, and no luck (there is a related, private “getValues” method).
So I’m guessing that some compiler/interpreter-foo is already going on to create a class that extends Enum out of a declaration like:
public static enum MyEnum
So is values() also statically translated into a hardcoded array during compilation? Or is it actually a method called at runtime, and if so, where is it defined?
The
values()method is part of the definition of theenumtype. Not to be confused with theEnumbase class. The formal definition is in the Section 8.9 of the JLS which does specify the order returned matches the order in which they are declared.