Enums in Java: From Basics to Advanced Concepts π
Java offers many features that help developers write safe, readable, and maintainable code. Enum (Enumeration) is one such powerful feature. Although enums look simple, they are much more capable than just holding constants.
This article explains Enums in Java from beginner to advanced level, with clear examples and real-world relevance.
1. What is an Enum in Java? (Basic Level)
An enum is a special data type used to define a fixed set of constants.
Simple Definition:
An enum represents a group of predefined values that do not change.
Examples:
Days of the week
Directions (NORTH, SOUTH, EAST, WEST)
Order status (PLACED, SHIPPED, DELIVERED)
2. Why Enums Were Introduced?
Before Java 5, constants were defined using:
public static final int MONDAY = 1;
public static final int TUESDAY = 2;
Problems:
β Not type-safe
β Invalid values allowed
β Hard to maintain
Enums solve these problems by providing type safety and clarity.
3. Basic Enum Example
enum Day {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}
Using the enum:
public class TestEnum {
public static void main(String[] args) {
Day today = Day.MONDAY;
System.out.println(today);
}
}
Output
MONDAY
4. Enum vs Normal Constants
Using Constants β
int day = 10; // Invalid but allowed
Using Enum β
Day day = Day.MONDAY; // Only valid values allowed
Benefits of Enum:
β Compile-time checking
β Cleaner code
β Easy to understand
β No invalid values
5. Using Enum with Switch Statement
Enums work seamlessly with switch.
Day day = Day.FRIDAY;
switch (day) {
case MONDAY:
System.out.println("Week starts");
break;
case FRIDAY:
System.out.println("Weekend is near");
break;
default:
System.out.println("Midweek");
}
6. Important Internal Concept of Enum π₯
Behind the scenes:
Every enum extends
java.lang.EnumEnums are special classes
Enum constants are objects
Key Restrictions:
β Enum cannot extend another class
β Enum can have variables, methods, and constructors
7. Enum with Fields and Constructor (Intermediate Level)
Enums can store data just like classes.
enum Level {
LOW(1),
MEDIUM(2),
HIGH(3);
private int value;
Level(int value) {
this.value = value;
}
public int getValue() {
return value;
}
}
Usage:
public class Test {
public static void main(String[] args) {
Level level = Level.HIGH;
System.out.println(level.getValue());
}
}
Output
3
π Note:
Enum constructors are private by default and cannot be called directly.
8. Built-in Enum Methods (Very Important β)
Java provides useful methods automatically:
Day d = Day.MONDAY;
d.name(); // MONDAY
d.ordinal(); // 0
Day.valueOf("MONDAY");
Day.values();
Looping through enum values:
for (Day day : Day.values()) {
System.out.println(day);
}
β Avoid using ordinal() for business logic.
9. Enum with Methods (Advanced Level)
Enums can have methods and logic.
enum Operation {
ADD,
SUB;
public int apply(int a, int b) {
switch (this) {
case ADD:
return a + b;
case SUB:
return a - b;
default:
return 0;
}
}
}
Usage:
public class Calculator {
public static void main(String[] args) {
System.out.println(Operation.ADD.apply(10, 5));
}
}
10. Enum with Constant-Specific Behavior (Deep Concept π‘)
Each enum constant can behave differently.
enum TrafficLight {
RED {
public String action() {
return "Stop";
}
},
GREEN {
public String action() {
return "Go";
}
},
YELLOW {
public String action() {
return "Ready";
}
};
public abstract String action();
}
Usage:
public class Test {
public static void main(String[] args) {
System.out.println(TrafficLight.RED.action());
}
}
11. Real-World Use Cases of Enum
Enums are widely used in real applications:
β Order Status (PLACED, SHIPPED, DELIVERED)
β User Access Levels
β Payment Status
β Application Modes
β Machine Learning labels
Example:
enum OrderStatus {
PLACED, SHIPPED, DELIVERED, CANCELLED
}
12. Common Mistakes with Enums β
β Using ordinal() in database logic
β Comparing enum values with strings
β Using enum when values are dynamic
13. Best Practices for Enums β
β Use enums only for fixed values
β Add fields instead of relying on position
β Keep enum names meaningful
β Prefer enum over constant integers