Do Java Enums Have Values? Explained

How-to basics · Updated November 23, 2026

Yes, Java enums have values. Every enum constant is an object with a name, an ordinal (its position starting at 0), and you can attach your own fields and methods to it. But the more useful answer is that you can give an enum any values you want by adding a constructor, fields, and getter methods — and you should, because that is how you store meaningful data like labels, codes, or numeric amounts directly on each constant.

What an Enum Is in Java

An enum (short for enumeration) is a special class type introduced in Java 5. It defines a fixed set of named constants. For example:

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

Here each constant is an instance of the Day class. The compiler automatically gives each one a name() method (returns the string “MONDAY”) and an ordinal() method (returns 0 for MONDAY, 1 for TUESDAY, etc.). Those are the built-in values every enum has. But they are rarely what you want to use in real code, because the name is just the identifier and the ordinal is fragile — if you reorder constants, the ordinal changes.

Adding Your Own Values to an Enum

To store meaningful values, you define fields, a constructor, and getters. Here is a common pattern:

public enum Status {
    NEW(1, "New order"),
    PROCESSING(2, "Being prepared"),
    SHIPPED(3, "On the way"),
    DELIVERED(4, "Arrived");

    private final int code;
    private final String description;

    Status(int code, String description) {
        this.code = code;
        this.description = description;
    }

    public int getCode() { return code; }
    public String getDescription() { return description; }
}

Now each constant carries an integer code and a human-readable description. You can access them like Status.SHIPPED.getCode() which returns 3. The constructor is called once per constant at class load time, so the values are fixed and immutable if you keep the fields final.

Common Ways to Use Enum Values

Once you have values, you often need to look up an enum from a value. A typical approach is a static lookup method using a loop or a stream:

public static Status fromCode(int code) {
    for (Status s : Status.values()) {
        if (s.code == code) return s;
    }
    throw new IllegalArgumentException("Unknown code: " + code);
}

This is useful when reading from a database or a file where you only have the numeric code. You can also use a Map for better performance if the enum is large, but for most enums (fewer than 10 constants) a simple loop is fine.

Enum Values vs. Regular Class Fields

People often confuse the word “values” with the built-in values() method. That method returns an array of all constants in declaration order. For example, Day.values() returns an array containing MONDAY through SUNDAY. That is different from the custom values you define. So when someone asks “do Java enums have values?”, the answer is yes in two senses: they have the implicit name and ordinal, and they can have explicit fields you define.

Another common misconception is that enums are just integers like in C or C++. In Java, they are full objects, so you can call methods on them, override methods, and even implement interfaces. But they cannot be subclassed — the compiler makes them final implicitly. You also cannot instantiate them with new; the only instances are the constants you declare.

Choosing Between Enum Values and Other Approaches

When should you use an enum with values versus a constant map or a separate class? Here are the trade-offs:

  • Use an enum with values when the set of constants is fixed at compile time and you want type safety (e.g., a status, a type, a category). It is self-documenting and you can add behavior.
  • Use a Map or a regular class when the set of values is dynamic, comes from a database, or changes frequently. Enums are compile-time constants, so adding a new constant requires recompiling.
  • Use a static final constant when you only need a single value like a string or number, not a set of related constants.

For example, a color palette with a fixed set of colors is a good enum. A list of product prices that changes daily should not be an enum.

Common Problems and Pitfalls

One frequent issue is relying on ordinal() for persistence. If you insert a new constant in the middle, all ordinals shift, corrupting any stored data. Always use a custom code or the name() string when saving to a database.

Another pitfall is using mutable fields in an enum. Since enums are singletons, any mutable state is shared across the entire JVM. If you must have mutable data, use a regular class instead. Also, be careful with values() — it returns a new array each time, so iterating is fine, but you cannot modify the array to affect the enum.

Finally, remember that an enum’s constructor cannot access static fields of the enum itself because the constants are created before static fields are initialized. This is a subtle but real constraint.

Enum Values in Switch Statements

Enums work beautifully with switch statements. You can switch on an enum constant directly:

switch (status) {
    case NEW:
        System.out.println("Just placed");
        break;
    case SHIPPED:
        System.out.println("In transit");
        break;
    default:
        System.out.println("Other");
}

This is more readable than switching on an integer code. The compiler checks that you handle all constants if you use a switch expression (Java 14+), which reduces bugs.

Performance and Memory Considerations

Enums are very lightweight. Each constant is a single object created once at class load. There is no per-use allocation cost. The values() method creates a new array on each call, which is a minor overhead, but for typical use (calling it a few times) it is negligible. If you call values() in a tight loop, consider caching the array in a static field. Also, since enums are singletons, they are safe to use in concurrent code without synchronization — they are inherently thread-safe.

Alternatives to Enums with Values

If you need more flexibility, consider a record (Java 16+) or a regular class with static constants. For example:

public record ProductType(String code, String label) {
    public static final ProductType BOOK = new ProductType("BK", "Book");
    public static final ProductType TOY = new ProductType("TY", "Toy");
}

This gives you similar benefits but allows you to create new instances at runtime. However, you lose the built-in values() method and the switch-case exhaustiveness checking. For most fixed sets, an enum is the better choice.

Frequently Asked Questions

Can an enum have multiple values per constant? Yes, you can have as many fields as you want. For example, a constant could have an int code, a String label, and a boolean flag. Just add them to the constructor.

How do I get the enum from its value? Write a static method that iterates over values() and compares the field, or use a Map built in a static initializer. For example, Status.fromCode(2) would return PROCESSING.

Is it safe to use enum ordinal as a database ID? No, it is not safe because reordering constants changes the ordinal. Use a custom code field that you control, or store the name() string.

Can I add methods to an enum? Yes, you can add any methods, including abstract methods that each constant overrides. This is a powerful pattern for behavior that varies by constant.

Do enums have a default value like null? No, an enum variable can be null if you assign null, but there is no default constant. You must always initialize it or handle null explicitly.

Can I compare enums with ==? Yes, because each constant is a singleton, == and equals() are equivalent for enums. Using == is safe and recommended.

Related

Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.

More in How-to basics

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.