Close

What Java enums are compiled to?

[Updated: May 20, 2018, Created: May 20, 2018]

Java Enum Java 

Java enums are syntactic sugar.
Java compiler compiles an enum class to a simple Java class extending java.lang.Enum and adds public static final variables for each enum constant.

Example

We are going to create a simple enum class, compile that and then disassemble the class using javap command:

public enum Fruit {
  Apple, Banana, Orange
}
D:\enum-compile-test>javac Fruit.java
D:\enum-compile-test>javap Fruit
Compiled from "Fruit.java"
public final class Fruit extends java.lang.Enum<Fruit> {
public static final Fruit Apple;
public static final Fruit Banana;
public static final Fruit Orange;
public static Fruit[] values();
public static Fruit valueOf(java.lang.String);
static {};
}

See Also