Stop 06 · Core Java APIsTopics · 4/8

Enums

Part of the Java Roadmap

In short

Order statuses, days of the week, user roles and payment modes all have a fixed set of values. Enums let the compiler guarantee you only ever use one of those values, so a typo like "SHIPED" can't slip in.

Enums define a fixed set of named constants, like days or order statuses. Java enums are full classes and can have fields, constructors and methods.

The jaadu stands at a busy junction beside a traffic signal, pointing one by one at its three fixed lights, red, yellow and green.

Think of it like this

The traffic signal

A traffic signal has exactly three lights: red, yellow and green. There is no purple, and nobody can invent one at the junction. Each light also carries meaning (stop, wait, go). A Java enum is that signal: a fixed list of named values, each able to carry its own data and behaviour.

Watch it happen

OrderStatus.values() in order

PLACED
0
PACKED
1
SHIPPED
2
DELIVERED
3
CANCELLED
4

1/4values() returns every constant, in the order you declared them.

The big ideas

What you need to know

A fixed set of constants

enum OrderStatus { PLACED, PACKED, SHIPPED } creates exactly those objects. You can't create new ones with new.

Enums are classes

They can have fields, a constructor (always private) and methods. Each constant can carry data like a label or a flag.

Perfect for switch

A switch expression over an enum that covers every constant needs no default, and the compiler warns if you miss one.

Safe to compare with ==

Each constant exists only once, so status == OrderStatus.SHIPPED is correct and fast.

See it in code

Walk through the code

java
1public enum OrderStatus {2    PLACED("Order placed", false),3    PACKED("Packed at warehouse", false),4    SHIPPED("On the way", false),5    DELIVERED("Delivered", true),6    CANCELLED("Cancelled", true);7 8    private final String label;9    private final boolean finished;10 11    OrderStatus(String label, boolean finished) {12        this.label = label;13        this.finished = finished;14    }15 16    public String label() { return label; }17    public boolean isFinished() { return finished; }18 19    public static void main(String[] args) {20        OrderStatus s = OrderStatus.valueOf("SHIPPED");21        String msg = switch (s) {22            case PLACED, PACKED -> "Getting ready in Bhiwandi";23            case SHIPPED -> "Out for delivery in Thane";24            case DELIVERED -> "Enjoy your order!";25            case CANCELLED -> "Refund started";26        };27        System.out.println(s.label() + ": " + msg);28        for (OrderStatus st : OrderStatus.values()) {29            System.out.println(st.ordinal() + " " + st + " finished=" + st.isFinished());30        }31    }32}

1/4Five constants, each passing a label and a finished flag to the constructor.

An order-tracking enum with data per constant and an exhaustive switch expression (Java 14+).

Save yourself the pain

Common mistakes

Oops

Storing statuses as plain strings like "shipped" or int codes like 3.

Do this instead

Use an enum. The compiler then rejects typos and invalid values.

Oops

Saving ordinal() to a database or file.

Do this instead

Save name() instead. Reordering or inserting a constant changes every ordinal and corrupts old data.

Oops

Calling valueOf() on raw user input without handling errors.

Do this instead

valueOf throws IllegalArgumentException for unknown names. Trim and uppercase the input, and catch the exception.

Your mission

Model payment modes

~20 min

Quick check

Why is it safe to compare enum values with == ?

Go deeper

  1. 1Create an OrderStatus enum and use it in a switch.
  2. 2Add a field and constructor to an enum, like a display label.
  3. 3Loop over all values with values().

Also part of these roadmaps

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge