Recommended Free Tools
iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more
A Java enum defines a type with a fixed set of named constants, such as the days of the week or the states in a workflow. Use one when the valid choices are known in advance: the compiler can then help prevent values outside that set. Enums can also hold fields and methods, so they can represent more than a list of labels.
What is an enum in Java?
An enum declaration creates a restricted class whose instances are the constants named in the declaration. Oracle recommends enums for a complete set of values known at compile time, including days, menu choices, and command-line flags. See Oracle’s enum tutorial and the Java SE 22 Language Specification.
public enum Day {
SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
THURSDAY, FRIDAY, SATURDAY
}
Declare a variable using the enum type, and assign it one of that type’s constants:
Day today = Day.MONDAY;
Unlike a free-form string, a Day variable cannot be assigned an arbitrary value such as "Funday". The available values are explicit in the type declaration.
How do you use an enum in Java?
Use constants with the enum type name, and branch on them with switch. This example uses arrow-form switch labels, supported in Java 14 and later as a permanent language feature.
Day day = Day.MONDAY;
switch (day) {
case MONDAY -> System.out.println("Start of the work week");
case FRIDAY -> System.out.println("End of the work week");
default -> System.out.println(day);
}
For Java versions before 14, use the older colon-and-break switch statement form instead. The Oracle enum tutorial is marked as JDK 8-era material, so its examples may not use newer syntax; consult the documentation for the Java baseline your project targets.
Rank #2
How do values() and valueOf() work?
List every constant with values()
The compiler provides values(), which returns the enum constants in the order they were declared. A for-each loop is a common way to process them:
The Tool Desk
Outbyte PC Repair FREERepair Windows errors before they cause bigger problemsFix Now →Outbyte Driver Updater FREEScan for outdated or missing drivers - takes under a minuteDriver Scan →for (Day day : Day.values()) {
System.out.println(day);
}
Look up a constant with valueOf()
Day.valueOf(String) looks up the constant whose declared name exactly matches the supplied string. Matching is case-sensitive and does not trim whitespace. A name that does not match a constant causes IllegalArgumentException; a null argument causes NullPointerException. For input from a user or external system, validate it or catch the relevant exception rather than assuming the name is valid. See the Java SE 26 Enum API.
Day selected = Day.valueOf("MONDAY");
// Day.valueOf("monday") throws IllegalArgumentException
Can an enum have a constructor, fields, and methods?
Yes. Each constant can supply arguments to the enum’s constructor, and the enum can define fields and methods that use those values. The constructor initializes the declared constants; application code cannot call it directly to create additional enum instances.
public enum Planet {
EARTH(5.972e24, 6.371e6),
MARS(6.417e23, 3.3895e6);
private final double mass;
private final double radius;
Planet(double mass, double radius) {
this.mass = mass;
this.radius = radius;
}
public double mass() { return mass; }
public double radius() { return radius; }
}
Here, each planet constant is initialized with its own mass and radius. The fields are private final, while methods provide access to their values. Oracle’s enum tutorial uses the same pattern to demonstrate enum constructors, fields, and methods.
Rank #4
What do name(), toString(), and ordinal() return?
name()returns the exact identifier declared for the constant, such asMONDAY.toString()returns a string representation and may be overridden to provide a more readable label. Do not rely on it when you specifically need the declared identifier.ordinal()returns the constant’s zero-based position in the declaration. For example, the first constant has ordinal 0. Reordering constants changes these values, so an ordinal is not a durable business ID.
If an enum value must be stored or sent to another system, define an explicit stable code or use a deliberately chosen name, then map that identifier to the enum. The Java SE 26 Enum API documents name() and ordinal().
Can Java enums extend a class or implement an interface?
Every enum extends java.lang.Enum, so an enum cannot extend an application-defined class. It can implement interfaces, which is useful when enum constants need to provide one of several implementations behind a shared contract.
Best Value
The Java SE 22 language specification adds a less common detail: an enum with no constant-specific class bodies is implicitly final. If one or more constants have their own class body, the enum is implicitly sealed and permits those generated constant-specific subclasses. Most everyday enum declarations do not need to use this feature explicitly.
When should you choose an enum?
An enum is a good fit when the valid set is closed and known at compile time, and when naming those options as a type improves clarity and prevents accidental values. It is especially useful when each option needs associated data or behavior.
- Choose an enum for a fixed set such as a status, direction, or menu choice.
- Use a different representation when valid values must be added dynamically, such as values loaded from a database or configured by users.
- For persistence or external APIs, define stable identifiers rather than storing
ordinal()values. - For collections keyed by enum constants or sets of enum constants, consider
EnumMapandEnumSet, specialized collection types documented by the JavaEnumAPI.
The Java enum API also documents special serialization handling for enum constants. Serialization behavior does not make an ordinal a suitable external identifier; choose a stable representation for data that must remain meaningful across versions or systems.
Do these 3 things before closing this tab:
1Repair Windows errors before they cause bigger problems2Scan for outdated or missing drivers - takes under a minute3Clear out junk files and repair common Windows errors

