Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท Object-Oriented Concepts

Advanced enums in Java

Advanced Java enums: fields and constructors, constant-specific bodies, abstract methods, interfaces, EnumMap and EnumSet, switch and singleton use.

A Java enum is a class with a fixed set of instances, declared once as the constants in its first line. Unlike enums in many languages, each constant can carry fields, be built with a constructor, implement an interface and even have its own method bodies. Enums are final (except via constant bodies), serializable, comparable by declaration order, and safe to compare with ==.

Use an enum when a value comes from a closed set known at compile time: days, states, log levels, operations, strategies.

Java
enum Level {
    LOW(1), HIGH(10);
    private final int weight;
    Level(int weight) { this.weight = weight; }
    int weight() { return weight; }
}

Try it

Java Fields, constructors and abstract methods per constant

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 Interfaces, EnumMap, EnumSet and switch

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.

Members of every enum

Member Description
values() Static; returns a new array of all constants in declaration order.
valueOf(String) Static; returns the constant with that exact name, else throws IllegalArgumentException.
name() / toString() The declared identifier; toString() may be overridden, name() is final.
ordinal() Zero-based position in the declaration.
compareTo(E) Compares ordinals.
getDeclaringClass() The enum class (not the anonymous constant-body class).

Usage notes

  • Constructors are private and run once per constant when the class initializes. You cannot call new on an enum.
  • Constants are singletons, so == is correct and null-safe, and switch works on them directly. Enums also make a safe thread-safe singleton (enum Config { INSTANCE }).
  • Avoid persisting ordinal(). Reordering or inserting a constant changes it; store name() or an explicit code field instead.
  • Fields should be final. Mutable enum state is shared global state.
  • Static fields are not accessible from the constructor (they are initialized after the constants), except for compile-time constants. Use a static initializer or a lookup method.
  • Use EnumMap and EnumSet instead of HashMap/HashSet for enum keys: they are faster and ordered by declaration.
  • Enums cannot extend classes (they already extend java.lang.Enum) and cannot be extended, but they can implement interfaces and be members of sealed hierarchies.
  • Strategy pattern: put the behavior in the constant (abstract method or lambda field) instead of a switch scattered around the code.
  • Case labels use the bare constant name (case SMALL); Java 21 also allows qualified names in pattern switches.