Enums

An enum is a special type used to represent a fixed set of values.

Simple way to remember:

Enum = a class with predefined instances that you cannot create yourself.


1. Creating an Enum

enum Day {
    MONDAY,
    TUESDAY,
    WEDNESDAY,
    THURSDAY,
    FRIDAY,
    SATURDAY,
    SUNDAY
}

You can use the predefined values:

Day today = Day.MONDAY;

You cannot do:

Day day = new Day(); // ERROR

2. Comparing Enums

Use == to compare enum values.

Day today = Day.MONDAY;

if (today == Day.MONDAY) {
    System.out.println("Start of the week");
}

Output:

Start of the week

3. Enum with switch

Enums work very well with switch.

Day day = Day.FRIDAY;

switch (day) {
    case MONDAY:
        System.out.println("Monday");
        break;

    case FRIDAY:
        System.out.println("Almost weekend!");
        break;

    default:
        System.out.println("Another day");
}

With modern switch:

switch (day) {
    case MONDAY -> System.out.println("Monday");
    case FRIDAY -> System.out.println("Almost weekend!");
    default -> System.out.println("Another day");
}

4. Enums Can Have Fields and Methods

An enum can work almost like a normal class.

enum Day {
    MONDAY("MON"),
    TUESDAY("TUE"),
    FRIDAY("FRI");

    private final String shortName;

    Day(String shortName) {
        this.shortName = shortName;
    }

    public String getShortName() {
        return shortName;
    }
}

Use it:

System.out.println(Day.MONDAY.getShortName());

Output:

MON

Important

The enum constructor is not called using new.

Day.MONDAY

automatically uses the constructor.


5. name()

Returns the name of the enum constant.

System.out.println(Day.MONDAY.name());

Output:

MONDAY

6. ordinal()

Returns the position of the constant, starting from 0.

System.out.println(Day.MONDAY.ordinal()); // 0
System.out.println(Day.TUESDAY.ordinal()); // 1

For:

MONDAY   → 0
TUESDAY  → 1
WEDNESDAY → 2

Avoid using ordinal() as a permanent ID because changing the enum order changes the ordinal.


7. values()

Returns all enum constants.

for (Day day : Day.values()) {
    System.out.println(day);
}

Output:

MONDAY
TUESDAY
WEDNESDAY

8. valueOf()

Converts a string into an enum constant.

Day day = Day.valueOf("MONDAY");

System.out.println(day);

Output:

MONDAY

The name must match exactly:

Day.valueOf("monday"); // ERROR

9. Enums Can Have Different Behavior

Each enum constant can have its own implementation.

enum Operation {

    ADD {
        int calculate(int a, int b) {
            return a + b;
        }
    },

    MULTIPLY {
        int calculate(int a, int b) {
            return a * b;
        }
    };

    abstract int calculate(int a, int b);
}

Use it:

System.out.println(Operation.ADD.calculate(5, 3));      // 8
System.out.println(Operation.MULTIPLY.calculate(5, 3)); // 15

This is useful but is less common for beginners.


10. Enum as Singleton

An enum can also be used to create a singleton.

enum Database {
    INSTANCE;

    void connect() {
        System.out.println("Connected");
    }
}

Use it:

Database.INSTANCE.connect();

There is only one INSTANCE.


11. Important Rules

Cannot extend an enum

enum Day {
    MONDAY
}

// class MyDay extends Day {} // ERROR

Enums already extend java.lang.Enum.

Can implement interfaces

interface Printable {
    void print();
}

enum Day implements Printable {
    MONDAY;

    public void print() {
        System.out.println("Monday");
    }
}

Easy Rule

Use an enum when you have a fixed set of related values.

For example:

enum Status {
    PENDING,
    APPROVED,
    REJECTED
}

Instead of using arbitrary strings:

String status = "approved"; // Can contain typos

you get type safety:

Status status = Status.APPROVED;

So the main idea is:

Enum = fixed, predefined, type-safe values.