Enum Declaration and Constants in Java

Enums define a fixed set of named constants with type safety. They are useful when a value should come from a small, predefined set of alternatives.

An enum is a special Java type whose instances are a fixed set of named values. You declare the allowed values inside an `enum` declaration, such as `enum Status { NEW, ACTIVE, CLOSED }`.

Enum constants are instances of the enum type, not arbitrary strings or integers. This gives the compiler type information, so a method accepting `Status` cannot accidentally receive an unrelated value such as a `String`.

Each enum constant is an object and can have fields, constructors, and methods. Java automatically provides useful methods such as `values()` and `valueOf(String)` for enum types.

Enums work well with `switch` expressions and statements. For persistent data or APIs, however, consider how enum names are serialized and stored because renaming a constant can affect compatibility.

Key points

Interview questions

What is an enum in Java, and why would you use one?

An enum is a Java type that represents a predefined set of named constants. It provides type safety because a variable of the enum type can only refer to values of that enum. Enums make code clearer than using unrelated strings or numeric constants. They are especially useful for small, fixed domains such as statuses, directions, or categories.

What is the difference between an enum constant and a `String` value?

An enum constant belongs to a specific enum type, while a `String` is general text. For example, `Status.ACTIVE` has type `Status`, so an API accepting `Status` cannot receive an arbitrary string without an explicit conversion. A string may contain values outside the intended domain. Enums therefore provide stronger modeling and compiler checking for fixed choices.

What do `values()` and `valueOf()` do?

`values()` returns an array containing the enum constants in their declaration order. `valueOf(String)` returns the constant whose name exactly matches the supplied string. If no matching constant exists, `valueOf()` throws `IllegalArgumentException`. The input therefore needs to match the enum constant name exactly.

Can Java enums have constructors, fields, and methods?

Yes. An enum can define fields, constructors, and methods just like other Java types, with restrictions specific to enum construction. Enum constants can pass arguments to the enum constructor, allowing each constant to carry associated data. Enum constructors are not called directly with `new`; the enum constants are created by the Java runtime.

What is a common persistence problem with enum ordinals?

An enum's ordinal represents its position in the declaration, starting at zero. If an application persists that number and the declaration order later changes, the same stored number can refer to a different constant. That makes ordinal-based persistence fragile. Persisting an explicit stable representation, such as a deliberate code or name with appropriate compatibility rules, avoids coupling stored data to declaration order.