Java Enum Class
In Java, when you need a fixed set of constants—like days of the week, directions (NORTH, SOUTH, EAST, WEST), or traffic light signals—using integers or strings can be error-prone.
To solve this, Java provides the enum (enumeration) class, which is a special data type that represents a group of predefined constants.
In this guide, we’ll cover:
- What enums are
- Defining enums
- Adding fields, constructors, and methods
- Using enums in switch statements
- Examples with code
What Is an Enum in Java?
- An enum is a special Java class that represents a group of constants.
- Enums are type-safe, meaning you can’t assign invalid values.
- Internally, each enum constant is an object of the enum type.
Example: Basic Enum
public enum Day {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}
public class EnumExample {
public static void main(String[] args) {
Day today = Day.MONDAY;
System.out.println(“Today is ” + today);
}
}
Output:
Today is MONDAY
Enum with Switch Statement
Enums work seamlessly with switch statements.
public class SwitchEnumExample {
enum Level { LOW, MEDIUM, HIGH }
public static void main(String[] args) {
Level level = Level.HIGH;
switch (level) {
case LOW:
System.out.println(“Low level”);
break;
case MEDIUM:
System.out.println(“Medium level”);
break;
case HIGH:
System.out.println(“High level”);
break;
}
}
}
Output:
High level
Enum with Fields, Constructors, and Methods
Enums can have:
- Fields (attributes)
- Constructors (always private)
- Methods (custom behavior)
Example: Traffic Light Enum
public enum TrafficLight {
RED(30), YELLOW(5), GREEN(25);
private int duration; // field
// constructor
TrafficLight(int duration) {
this.duration = duration;
}
// method
public int getDuration() {
return duration;
}
}
public class TrafficLightExample {
public static void main(String[] args) {
for (TrafficLight light : TrafficLight.values()) {
System.out.println(light + ” lasts ” + light.getDuration() + ” seconds.”);
}
}
}
Output:
RED lasts 30 seconds.
YELLOW lasts 5 seconds.
GREEN lasts 25 seconds.
Enum Methods
- values() → returns an array of all enum constants.
- valueOf(String name) → returns the enum constant with the given name.
- ordinal() → returns the position of the constant (0-based).
Example:
public class EnumMethodsExample {
enum Direction { NORTH, SOUTH, EAST, WEST }
public static void main(String[] args) {
for (Direction d : Direction.values()) {
System.out.println(d + ” at index ” + d.ordinal());
}
Direction dir = Direction.valueOf(“EAST”);
System.out.println(“Chosen direction: ” + dir);
}
}
Advantages of Enums
- Type-safe alternative to integers or strings.
- Can include fields, methods, and constructors.
- Easy to use in switch statements.
- Prevents invalid constant values.
Best Practices
- Use enums for fixed sets of constants.
- Add fields and methods when constants need associated values.
- Use switch with enums for cleaner conditional logic.
- Prefer enums over integer/string constants for readability and safety.
Conclusion
Java enum classes provide a powerful way to define constant groups in a type-safe manner.
- Use enums for fixed categories like days, levels, and directions.
- Enhance enums with fields, methods, and constructors for more flexibility.
- Use enums in switch statements for clean logic.
By mastering enums, you’ll make your Java code more readable, maintainable, and error-free.