Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท Types and Literals

Java enum types

Reference for Java enum types: constants, fields, constructors, abstract methods, values(), valueOf(), EnumMap, EnumSet and switch usage.

An enum type is a class with a fixed, named set of instances. Each constant is created once, when the enum class is initialized, and can never be created again. That makes enums the right tool for things like days of the week, states of a state machine or log levels, where a String or an int constant would let invalid values slip in.

Because an enum is a real class, its constants can carry fields, run constructor code and override methods. Every enum implicitly extends java.lang.Enum, which supplies name(), ordinal(), compareTo() and friends. The keyword itself is covered in enum; this page looks at the type's behavior.

Java
enum Level {
    LOW(1), MEDIUM(5), HIGH(10);

    private final int weight;

    Level(int weight) { this.weight = weight; }

    int weight() { return weight; }
}

Try it

Java Constants, fields and values()

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Java Constant-specific bodies, EnumMap and EnumSet

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Syntax

Java
[modifiers] enum Name [implements Interface, ...] {
    CONSTANT [(args)] [{ body }], ...;
    // fields, constructors, methods
}
Member Description
static E[] values() Generated. A new array of all constants in declaration order.
static E valueOf(String name) Generated. The constant with exactly that name. Throws IllegalArgumentException if there is none and NullPointerException for null.
String name() The identifier as declared. Final.
int ordinal() Zero-based position in the declaration.
String toString() Returns name() unless overridden.
int compareTo(E other) Compares by ordinal.
Class<E> getDeclaringClass() The enum class, even for constants with bodies.

Usage notes

  • Constructors are implicitly private. You cannot call new on an enum, and a constructor cannot be public or protected.
  • Compare with ==. There is exactly one instance per constant, so == is safe, null-safe on the left side and as fast as it gets. equals() and hashCode() are final and use identity.
  • The constant list comes first and ends with a semicolon if anything else follows it.
  • Constants with bodies are anonymous subclasses, so getClass() may differ from getDeclaringClass(). Use the latter.
  • Enums can implement interfaces but cannot extend classes. They are implicitly final unless a constant has a body, in which case they are implicitly sealed.
  • values() allocates a copy every call. Cache it in a static field if you call it in a hot loop.
  • Do not rely on ordinal() for persistence. Reordering or inserting constants changes it. Store name() or an explicit code instead.
  • EnumMap and EnumSet are array and bit-vector based, so they are faster and more compact than HashMap and HashSet for enum keys, and iterate in declaration order.
  • Switch works on enums; with a switch expression the compiler can check that every constant is covered when you leave out default.
  • Serialization and valueOf are by name, so renaming a constant breaks stored data.
  • Static fields are not accessible from an enum constructor (the constants are created first). Put shared lookup maps in a nested holder class or a static initializer.