Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท java.lang

The Enum class

Reference for java.lang.Enum, the base class of every enum: name(), ordinal(), compareTo(), valueOf(), getDeclaringClass() and the pitfalls of ordinals.

java.lang.Enum<E> is the abstract superclass of every enum type. When you write enum Level { LOW, HIGH }, the compiler turns it into a class that extends Enum<Level>, creates one instance per constant, and adds the static methods values() and valueOf(String). You never extend Enum yourself: the compiler forbids it.

The class gives every constant a name, a position (its ordinal), a natural order based on that position, and identity-based equality. Because there is exactly one instance per constant, == is the right way to compare enum values.

Java
enum Level { LOW, MEDIUM, HIGH }

Level level = Level.MEDIUM;
level.name();      // "MEDIUM"
level.ordinal();   // 1

Try it

Java name, ordinal and ordering

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 valueOf, declaring class and constant bodies

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
public abstract class Enum<E extends Enum<E>>
        implements Constable, Comparable<E>, Serializable
Member Description
String name() The constant's identifier exactly as declared. Final.
int ordinal() Zero-based position in the declaration. Final.
String toString() Returns name() by default. You may override it for display text.
int compareTo(E other) Compares ordinals. Final. Both constants must have the same enum type.
Class<E> getDeclaringClass() The enum type, even for constants that have their own body.
static <T extends Enum<T>> T valueOf(Class<T> type, String name) Looks a constant up by name.
boolean equals(Object), int hashCode() Final. Identity equality; the hash code is identity-based, so it differs between runs.

The compiler adds two static methods to each enum type (they are not members of Enum):

Method Description
static E[] values() A new array with all constants in declaration order.
static E valueOf(String name) The constant with that exact name.

Throws: valueOf throws IllegalArgumentException when no constant has the name, and NullPointerException when the name is null.

Usage notes

  • Compare with ==. It is null-safe on the left side, checked at compile time for mismatched types, and as fast as it gets. equals() gives the same answer.
  • Do not store ordinal(). Reordering or inserting a constant silently changes every stored number. Persist name() or an explicit code field instead. Use ordinal() only for in-memory array indexes, and prefer EnumMap.
  • values() allocates. It clones an internal array on each call. Cache the result in a static final list if you call it in a hot loop.
  • getClass() can surprise you. A constant with a body is an instance of an anonymous subclass, so use getDeclaringClass() when you need the enum type.
  • Natural order is declaration order, so TreeSet, TreeMap and Collections.sort need no comparator for enums.
  • Enums are serializable by name, and clone() throws CloneNotSupportedException, so the one-instance-per-constant guarantee holds.
  • Enums can implement interfaces and have fields, constructors and methods. See advanced enums.