BackendBench
Coffee

Unravel โ€บ Java concepts โ€บ OOP and types

Java language

Enums

An enum is a fixed set of named constants, such as order statuses.

๐Ÿ”Ž Unravel this example

At a glance

Mental model

A multiple-choice answer sheet: only the listed options are valid, and the compiler rejects anything else.

What happens, in order
  1. 1enum Status { NEW, PAID, SHIPPED } creates exactly three instancesCompare with ==.
  2. 2Status.valueOf("PAID") parses a name (throws IllegalArgumentException for an unknown one) and Status.values() lists all
  3. 3Enums can have constructors, fields and abstract methods per constant, which replaces long if/else chains with behaviour attached to the value
  4. 4A switch over an enum with an arrow form and no default makes the compiler check that every constant is handled, so adding a new status later turns every forgotten switch into a compile error instead of a production bug
  5. 5To store or send an enum, use its name (status.name()), not its positionJSON libraries such as Jackson write the name by default, and JPA needs @Enumerated(EnumType.STRING) to do the same.
Remember
Watch out
Storing ordinalsUsing strings where an enum would make illegal values impossible
Example (highlighted lines explain it)
public enum Status {    NEW("n"), PAID("p"), SHIPPED("s");    private final String code;    Status(String code) { this.code = code; }    public static Status fromCode(String code) {        for (Status s : values()) {            if (s.code.equals(code)) {                return s;            }        }        throw new IllegalArgumentException("unknown code " + code);    }}
  1. line 8 uses values, the core of Enums
Read the full explanation (about 1 min)

An enum is a fixed set of named constants, such as order statuses. Each constant is a real object and can carry fields and methods.

A mental model

A multiple-choice answer sheet: only the listed options are valid, and the compiler rejects anything else.

How it works

enum Status { NEW, PAID, SHIPPED } creates exactly three instances. Compare with ==.

Status.valueOf("PAID") parses a name (throws IllegalArgumentException for an unknown one) and Status.values() lists all.

Enums can have constructors, fields and abstract methods per constant, which replaces long if/else chains with behaviour attached to the value.

A switch over an enum with an arrow form and no default makes the compiler check that every constant is handled, so adding a new status later turns every forgotten switch into a compile error instead of a production bug.

To store or send an enum, use its name (status.name()), not its position. JSON libraries such as Jackson write the name by default, and JPA needs @Enumerated(EnumType.STRING) to do the same.

Going deeper

Persisting an enum by its ordinal breaks when someone inserts a constant in the middle; store the name. EnumMap and EnumSet are compact and fast for enum keys.

Under the hood

Each constant is a static final instance created when the class loads; ordinal() is its declaration index. The compiler generates values() and valueOf for you.

Common mistakes

  • Storing ordinals.
  • Using strings where an enum would make illegal values impossible.

How Unravel recognises it

When you paste code, Unravel looks for these signs of enums. Spotting them yourself is a good habit when you read unfamiliar code:

Part of a learning path

Related concepts

Builds on this

Frequently asked questions

What is Enums?

An enum is a fixed set of named constants, such as order statuses. Each constant is a real object and can carry fields and methods.

What are the most common mistakes with Enums?

Storing ordinals. Using strings where an enum would make illegal values impossible.

How can I see Enums in real code?

Paste your own Java into Unravel and it will point out where enums appears, explain how it works at the detail level you choose, and draw the flow of the code around it.