Skip to content

Lesson 04 · Enums

Objectives

After this lesson you will be able to:

  • Declare enums with fields, constructors, and methods.
  • Use values(), valueOf(), ordinal(), and name().
  • Use enums in switch and give constants per-constant behavior.

Basics

An enum is a fixed set of named constant instances. Each constant is a singleton object of the enum type.

java
enum Day { MON, TUE, WED }
Day d = Day.MON;
d.name();        // "MON"
d.ordinal();     // 0   (declaration position, zero-based)
Day.valueOf("TUE");   // Day.TUE  — throws IllegalArgumentException if no match
Day.values();    // Day[] {MON, TUE, WED}  — a fresh array each call

Exam trap

valueOf is case-sensitive and throws IllegalArgumentException (not NumberFormatException) for an unknown name. Avoid relying on ordinal() for logic or persistence — reordering constants silently changes the numbers.

Fields, constructors, methods

An enum can have state and behavior. The constructor is implicitly private and runs once per constant, when the constant is created.

java
enum Planet {
    EARTH(5.97e24, 6.37e6),
    MARS(6.42e23, 3.39e6);          // constants first, with constructor args

    private final double mass, radius;
    Planet(double mass, double radius) {   // implicitly private
        this.mass = mass; this.radius = radius;
    }
    double gravity() { return 6.67e-11 * mass / (radius * radius); }
}
Planet.EARTH.gravity();

Gotcha

The constant list must come first in the body, ended with a semicolon before any fields/methods. An enum cannot be extended and cannot extend another class (it already extends java.lang.Enum), though it can implement interfaces.

Per-constant bodies and switch

Each constant may override a method with a constant-specific class body:

java
enum Op {
    PLUS  { public int apply(int a, int b) { return a + b; } },
    TIMES { public int apply(int a, int b) { return a * b; } };
    public abstract int apply(int a, int b);
}
Op.PLUS.apply(2, 3);   // 5

Enums shine in switch, where you reference constants by simple name:

java
String label = switch (d) {
    case MON, TUE, WED -> "weekday";   // not Day.MON — just MON
};

Key Takeaways

  • Each enum constant is a singleton instance; name() is its identifier, ordinal() its 0-based position (don't depend on it).
  • values() returns a fresh array; valueOf is case-sensitive and throws IllegalArgumentException on a miss.
  • Enums can have fields, methods, and a (private) constructor; constants are listed first.
  • Enums can implement interfaces and give per-constant behavior, but cannot be extended.
  • In switch, refer to constants by their simple name (MON, not Day.MON).

Lesson Quiz

Lesson Quiz · Enums0 / 5
  1. What does Day.valueOf("mon") do, given enum Day { MON, TUE }?

    • AReturns MON
    • BReturns null
    • CIllegalArgumentException
    • DNumberFormatException
  2. What is Day.WED.ordinal() given enum Day { MON, TUE, WED }?

    • A1
    • B2
    • C3
    • D"WED"
  3. An enum constructor is implicitly...

    • Apublic
    • Bprotected
    • Cprivate
    • Dstatic
  4. Which is TRUE about enums?

    • AAn enum can extend another class
    • BAn enum can implement interfaces
    • CConstants can be listed after methods
    • DvalueOf returns null for unknown names
  5. In a switch on a Day value, how do you reference the constant MON?

    • Acase Day.MON ->
    • Bcase MON ->
    • Ccase "MON" ->
    • Dcase Day::MON ->

Next: Records. Run the matching code in labs/src/main/java/com/jse21/m03_oop/.