🎯 Java Enums: Complete Guide
🏗️ What are Enums?
Section titled “🏗️ What are Enums?”Think of it like a category of predefined options.
Examples:
- Days of the week (MONDAY, TUESDAY, …)
- Directions (NORTH, SOUTH, …)
- Status codes (SUCCESS, ERROR)
- Traffic signals (RED, YELLOW, GREEN)
🔧 Basic Enum Syntax
Section titled “🔧 Basic Enum Syntax”Enum Declaration
Section titled “Enum Declaration”enum EnumName { CONSTANT1, CONSTANT2, CONSTANT3;}Simple Example
Section titled “Simple Example”enum Day { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY}🚀 Simple Examples
Section titled “🚀 Simple Examples”Basic Enum Usage
Section titled “Basic Enum Usage”enum Day { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY}
public class EnumExample { public static void main(String[] args) { Day today = Day.FRIDAY;
if (today == Day.FRIDAY) { System.out.println("Weekend is coming!"); }
// Loop through all enum values for (Day d : Day.values()) { System.out.println(d); } }}Output:
Weekend is coming!MONDAYTUESDAYWEDNESDAYTHURSDAYFRIDAYSATURDAYSUNDAY✅ Here,
Day.values()gives an array of all constants.
✅ Enums vs Constants
Section titled “✅ Enums vs Constants”Why Enums are Better
Section titled “Why Enums are Better”Unlike final static constants, enums can have:
- Fields (variables)
- Constructors
- Methods
- Type safety
- Built-in methods like
values(),valueOf(),ordinal()
🧬 Enum with Fields and Methods
Section titled “🧬 Enum with Fields and Methods”Traffic Lights Example 🚦
Section titled “Traffic Lights Example 🚦”enum TrafficSignal { RED("Stop"), YELLOW("Get Ready"), GREEN("Go");
private String action;
// Constructor TrafficSignal(String action) { this.action = action; }
// Getter public String getAction() { return action; }}
public class EnumWithMethods { public static void main(String[] args) { for (TrafficSignal signal : TrafficSignal.values()) { System.out.println(signal + " means " + signal.getAction()); } }}Output:
RED means StopYELLOW means Get ReadyGREEN means Go✅ Each enum constant has its own value and behavior.
🔄 Enum in Switch Statements
Section titled “🔄 Enum in Switch Statements”Direction Example
Section titled “Direction Example”enum Direction { NORTH, SOUTH, EAST, WEST}
public class EnumSwitch { public static void main(String[] args) { Direction dir = Direction.EAST;
switch (dir) { case NORTH: System.out.println("Going up!"); break; case SOUTH: System.out.println("Going down!"); break; case EAST: System.out.println("Going right!"); break; case WEST: System.out.println("Going left!"); break; } }}🔧 Enum with Overridden Methods (Advanced)
Section titled “🔧 Enum with Overridden Methods (Advanced)”Mathematical Operations Example
Section titled “Mathematical Operations Example”enum Operation { ADD { public int apply(int x, int y) { return x + y; } }, SUBTRACT { public int apply(int x, int y) { return x - y; } }, MULTIPLY { public int apply(int x, int y) { return x * y; } }, DIVIDE { public int apply(int x, int y) { return x / y; } };
public abstract int apply(int x, int y);}
public class EnumPolymorphism { public static void main(String[] args) { int a = 20, b = 5; for (Operation op : Operation.values()) { System.out.println(op + " → " + op.apply(a, b)); } }}Output:
ADD → 25SUBTRACT → 15MULTIPLY → 100DIVIDE → 4✅ Here, each enum constant overrides the
apply()method differently.
🔢 Understanding ordinal()
Section titled “🔢 Understanding ordinal()”What is ordinal()?
Section titled “What is ordinal()?”Every constant inside an enum has a position (index), starting from 0.
The ordinal() method returns that index number of the enum constant.
Basic Example
Section titled “Basic Example”enum Day { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY}
public class EnumOrdinal { public static void main(String[] args) { for (Day d : Day.values()) { System.out.println(d + " → ordinal = " + d.ordinal()); } }}Output:
MONDAY → ordinal = 0TUESDAY → ordinal = 1WEDNESDAY → ordinal = 2THURSDAY → ordinal = 3FRIDAY → ordinal = 4Key Points About ordinal()
Section titled “Key Points About ordinal()”- It always starts from 0 and increases in the order the constants are declared
- It is useful if you want to know the position of an enum constant
- ⚠️ Caution: You should not rely heavily on
ordinal()for business logic
Why ordinal() Can Be Problematic
Section titled “Why ordinal() Can Be Problematic”// ❌ Bad: Relying on ordinal for business logicenum BadPriority { LOW, MEDIUM, HIGH, CRITICAL; // Ordinal: 0, 1, 2, 3
public int getValue() { return ordinal() + 1; // Fragile! Returns 1, 2, 3, 4 }}
// ✅ Good: Explicit valuesenum GoodPriority { LOW(1), MEDIUM(2), HIGH(3), CRITICAL(4);
private final int value;
GoodPriority(int value) { this.value = value; }
public int getValue() { return value; // Always returns the intended value }}Real-life Example
Section titled “Real-life Example”enum Priority { LOW, MEDIUM, HIGH, CRITICAL}
public class EnumPriority { public static void main(String[] args) { Priority p = Priority.HIGH; System.out.println("Priority: " + p); System.out.println("Ordinal value: " + p.ordinal());
// Show all priorities with their ordinals for (Priority priority : Priority.values()) { System.out.println(priority + " → ordinal = " + priority.ordinal()); } }}Output:
Priority: HIGHOrdinal value: 2LOW → ordinal = 0MEDIUM → ordinal = 1HIGH → ordinal = 2CRITICAL → ordinal = 3💡 So,
ordinal()= the index (position) of the enum constant in its declaration.
🔑 Key Points to Remember
Section titled “🔑 Key Points to Remember”Essential Concepts
Section titled “Essential Concepts”-
Enums are type-safe → you can’t assign a wrong value
Day d = Day.MONDAY; // ✅ valid// Day d = 1; // ❌ error -
Enums can implement interfaces but cannot extend classes (because they implicitly extend
java.lang.Enum) -
Enums can have constructors, fields, and methods
-
Built-in enum methods:
values()- returns array of all enum constantsvalueOf(String)- converts string to enum constantordinal()- returns position (0-based index)
-
Type safety prevents invalid assignments
🎯 Practice Examples
Section titled “🎯 Practice Examples”Example 1: Order Status in E-commerce
Section titled “Example 1: Order Status in E-commerce”enum OrderStatus { PENDING("Order is pending confirmation", 1), CONFIRMED("Order has been confirmed", 2), SHIPPED("Order has been shipped", 3), DELIVERED("Order has been delivered", 4), CANCELLED("Order has been cancelled", 5);
private final String description; private final int statusCode;
OrderStatus(String description, int statusCode) { this.description = description; this.statusCode = statusCode; }
public String getDescription() { return description; } public int getStatusCode() { return statusCode; }
public boolean canCancel() { return this == PENDING || this == CONFIRMED; }
public OrderStatus next() { return switch (this) { case PENDING -> CONFIRMED; case CONFIRMED -> SHIPPED; case SHIPPED -> DELIVERED; case DELIVERED, CANCELLED -> this; // Final states }; }}
public class OrderExample { public static void main(String[] args) { OrderStatus status = OrderStatus.PENDING;
System.out.println("Current status: " + status); System.out.println("Description: " + status.getDescription()); System.out.println("Status code: " + status.getStatusCode()); System.out.println("Can cancel: " + status.canCancel());
// Process order status = status.next(); System.out.println("Next status: " + status); }}Example 2: Log Levels with Custom Numbering
Section titled “Example 2: Log Levels with Custom Numbering”enum LogLevel { DEBUG(0, "DEBUG"), INFO(1, "INFO"), WARN(2, "WARN"), ERROR(3, "ERROR"), FATAL(4, "FATAL");
private final int level; private final String name;
LogLevel(int level, String name) { this.level = level; this.name = name; }
public int getLevel() { return level; } public String getName() { return name; }
public boolean isEnabled(LogLevel minimumLevel) { return this.level >= minimumLevel.level; }
public static LogLevel fromString(String level) { try { return LogLevel.valueOf(level.toUpperCase()); } catch (IllegalArgumentException e) { return INFO; // Default fallback } }
// Compare with ordinal() vs custom level public void showComparison() { System.out.println(this.name + ":"); System.out.println(" Ordinal: " + this.ordinal()); System.out.println(" Custom Level: " + this.level); }}🧩 Quick Reference Flashcards
Section titled “🧩 Quick Reference Flashcards”| Concept | Summary |
|---|---|
| Enum Declaration | enum Name { CONSTANT1, CONSTANT2 } |
| values() | Returns array of all enum constants |
| ordinal() | Returns position (0-based index) |
| Type Safety | Prevents invalid constant assignments |
| Fields & Methods | Can have constructors, fields, and methods |
| Switch Statements | Perfect for switch expressions |