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

Java enum types represent a small, fixed set of named choices. They give each choice a real type, allow safe comparisons and switch statements, and can carry fields and methods when a choice has associated data. The Stranger Things names below are only illustrative; they do not change how Java enums work.

What a Java enum is

Oracle’s Java tutorial defines an enum as “a special data type that enables for a variable to be a set of predefined constants.” In the Java Language Specification, an enum declaration is a restricted class type whose declared constants are instances of that type.

Use an enum when the valid choices are known and intentionally closed, such as a game state, menu option, day of the week, or character role:

enum HawkinsRole {
    ELEVEN, MIKE, DUSTIN, LUCAS, MAX
}

HawkinsRole favorite = HawkinsRole.ELEVEN;

favorite can hold a HawkinsRole, not an arbitrary string such as "Steve". A string would need explicit validation and conversion before it could become an enum value.

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

Declare, assign, and compare enum constants

Reference a constant with the enum type followed by a dot. Constants of the same enum type can be compared with ==; this is appropriate because each declared constant is a unique enum instance.

HawkinsRole role = HawkinsRole.MAX;

if (role == HawkinsRole.MAX) {
    System.out.println("Grab the skateboard");
}

A switch can branch directly on the enum value. Inside the switch, use the constant name without repeating the enum type:

switch (role) {
    case ELEVEN:
        System.out.println("Psychic powers");
        break;
    case DUSTIN:
        System.out.println("Inventive problem-solving");
        break;
    default:
        System.out.println("Another Hawkins role");
}

On newer Java versions, a switch expression can return a value, but the same enum constants and type rules apply.

Iterate over every declared value

Every enum type receives a values() method. It returns an array containing the constants in their declaration order, which is useful for menus, tests, and displaying all choices:

Free tools Windows power users keep installed

One-click scans. No signup required.

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

The order is the source-code declaration order. Treat it as presentation order, not as a permanent business identifier.

Give constants data and behavior

An enum body can contain fields, a constructor, methods, and other members. Each constant can pass arguments to the constructor. Put a semicolon after the constant list when additional members follow.

enum HawkinsRole {
    ELEVEN("psychic"),
    DUSTIN("inventor"),
    MAX("skateboarder");

    private final String description;

    HawkinsRole(String description) {
        this.description = description;
    }

    public String description() {
        return description;
    }
}

Then call HawkinsRole.ELEVEN.description() to retrieve the associated text. This pattern is often clearer than maintaining parallel arrays or scattering related logic through multiple switch statements.

Modeling matchups or weaknesses

If a category has a known relationship to another category, store that relationship in the enum itself. For a simple one-to-one matchup:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
enum CreatureType {
    DEMOGORGON("fire"),
    MIND_FLAYER("electricity"),
    DEMOBAT("cold");

    private final String weakness;

    CreatureType(String weakness) {
        this.weakness = weakness;
    }

    public String weakness() {
        return weakness;
    }
}

For several weaknesses, use a collection field such as Set<String> and initialize it in each constant. If the relationships are user-created, change over time, or arrive from a file or service, a database-backed or validated class may be a better model than a compile-time enum.

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

Enum versus strings or integer constants

Approach Best fit What you give up
enum A closed set known when the program is compiled; invalid choices should be rejected by the type system. Adding a new value requires changing and redeploying the code.
Validated String Values come from users, files, APIs, or another open-ended external system. Validation, spelling, case handling, and unknown-value behavior are your responsibility.
Integer constants Interoperability with an existing numeric protocol that requires fixed codes. Integers are easy to mix up and do not communicate the domain as clearly as an enum.

Choose a plain enum when the names are enough. Add fields or methods when each choice owns data or behavior that belongs naturally with it.

Why ordinal() is not a durable ID

The java.lang.Enum API defines ordinal() as a constant’s position in its declaration, starting at zero:

int position = HawkinsRole.MAX.ordinal();

Inserting or reordering constants changes that number. Do not store it in a database, send it as an API code, or treat it as a stable identifier. Define an explicit field instead:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
enum HawkinsRole {
    ELEVEN("ELV"),
    MIKE("MIK"),
    DUSTIN("DUS");

    private final String code;

    HawkinsRole(String code) {
        this.code = code;
    }

    public String code() {
        return code;
    }
}

Rules that explain common compiler errors

  • The constants are the enum’s instances. Ordinary code cannot add another one with new; enum constructors are used only while Java creates the declared constants.
  • An enum implicitly derives from java.lang.Enum, so it cannot extend another class. It can implement interfaces.
  • A nested enum is implicitly static.
  • In Java SE 26, an enum without constant-specific class bodies is implicitly final. If one or more constants has its own class body, the enum is implicitly sealed and those generated classes are its permitted direct subclasses.

A frequent beginner mistake is trying to assign extra properties after declaring a bare list of constants, or trying to assign a string directly to an enum variable. Put the property in a field and constructor, as in the matchup example, and convert external text through a deliberate lookup method rather than an unchecked assignment.

When an enum is the wrong abstraction

  • The set of values is created by users or administrators.
  • New values arrive from a service without a code release.
  • Each value has a large, changing data record better represented by a class or database row.
  • The external system’s numeric or textual code must remain independent of Java declaration order.

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.