Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Java enums define a fixed set of named values as a distinct type. Use them when a variable should hold one of a known group of choices, such as days of the week or HTTP statuses. The examples below use Java 14+ switch arrow syntax; a traditional switch is included for projects on older source levels.

Declare an enum and use its constants

Declare an enum with the enum keyword and list its constants. Each constant is an instance of that enum type, so the compiler can reject unrelated values.

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

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

Refer to constants through the enum type, as in Day.MONDAY. Enum classes extend java.lang.Enum. See JLS §8.9 and Oracle’s Dev.java enums guide.

Iterate through every enum constant

Every enum has an implicitly declared values() method. It returns the constants in the order they were declared.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
for (Day day : Day.values()) {
    System.out.println(day);
}

This is useful for building menus or checking every supported option without maintaining a separate list.

Convert a string to an enum value

Use valueOf when a string is expected to match a constant’s declared name exactly.

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

The match is case-sensitive and does not ignore whitespace: "monday" and " MONDAY " do not match MONDAY. An unknown name throws IllegalArgumentException, so catch or validate that case when the string comes from a user or external system.

try {
    Day day = Day.valueOf(input);
    System.out.println("Selected: " + day);
} catch (IllegalArgumentException e) {
    System.out.println("Unknown day: " + input);
}

The API specifies that “The name must match exactly an identifier used to declare an enum constant in this class.” See the Java SE Enum API.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Branch on an enum with switch

In Java 14 and later, arrow-form switch labels can group constants and avoid fall-through:

switch (today) {
    case MONDAY -> System.out.println("Work week");
    case SATURDAY, SUNDAY -> System.out.println("Weekend");
    default -> System.out.println("Other weekday");
}

For projects using older Java source levels, use the traditional colon form and terminate each case with break to prevent fall-through:

switch (today) {
    case MONDAY:
        System.out.println("Work week");
        break;
    case SATURDAY:
    case SUNDAY:
        System.out.println("Weekend");
        break;
    default:
        System.out.println("Other weekday");
}

Use the syntax supported by your project’s configured source level. Oracle documents the language changes in its Java Language Updates.

Give constants associated data

An enum can declare fields, a constructor, and methods. Put a semicolon after the constant list when declarations follow it. Java invokes the constructor for each declared constant; application code cannot call it to create additional enum instances.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
enum HttpStatus {
    OK(200), NOT_FOUND(404);

    private final int code;

    HttpStatus(int code) {
        this.code = code;
    }

    int code() {
        return code;
    }
}

int notFoundCode = HttpStatus.NOT_FOUND.code();

This keeps the named option and its associated value together. The JLS and Dev.java guide cover enum constructors and members.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Choose between switch logic and enum-specific behavior

A switch is a good fit when the operation belongs to a caller or varies by context. Put behavior on the enum when it is intrinsic to each constant and should travel with its data. For example, if each status must supply its own description, the enum can expose that through a method; if several separate operations interpret statuses differently, keeping those operations outside may be clearer. The JLS notes that a switch can simulate adding a method to an enum from outside its declaration.

Do not treat ordinal as a stable identifier

ordinal() is the constant’s zero-based position in the declaration. Reordering or inserting constants changes those positions, and compareTo() also orders constants by declaration position. Do not persist an ordinal as a database code or use it as a durable business identifier. Store an explicit field such as the code in the HTTP-status example instead.

name() returns the exact declared identifier. toString() normally returns that name, but an enum may override it for display, so use name() when the identifier representation itself is required. See the Enum API.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.