To put it simply, enumerable classes in Java are weird.
Writing vs. Compiled
Enumerable classes get transformed from being an enum type into a class type (final, extends Enum) at the point of compilation.
Example (No-Arguments Constructor)
Pre-compilation
public enum Example {
ONE, TWO, THREE, FOUR
}
Decompiled
public final class Example extends java.lang.Enum<Example> {
public static final Example ONE;
public static final Example TWO;
public static final Example THREE;
public static final Example FOUR;
private static final Example[] $VALUES;
public static Example[] values();
public static Example valueOf(java.lang.String);
private Example();
private static Example[] $values();
static {};
}
Help
Decompiled via
javap -p Example.class. See javap for more information on how to use javap.
Comparison
Taking a look at the pre-compiled and decompiled code - it is quite apparently different. This is because the enum type is not a part of the JVM instruction set.
When referencing an enumerable field - you’re just referencing an instance of the enum.
Understanding generated fields/methods
Note
All of these assume that you have not added any new fields/methods. Those ae treated as regular and do not need any additional clarification.
Static Fields
Within an enum there is static fields:
- being the number of enumerable instances (instance of the class itself - i.e.,
ONE,TWO,THREE, andFOURfrom Understanding Enum Classes) - being
$VALUES- a static field which stores references to all enumerable instances within.