Java enums (short for enumerations) are special classes representing a group of constants (unchangeable variables, like final variables). Enums improve type safety, make code easier to read, and eliminate the need for traditional public static final
constants. Let’s explore Java enums with examples and comments to illustrate their usage.
Basic Enum Declaration and Usage
Enums are defined using the enum
keyword. Here’s a simple enum representing the days of the week:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
enum Day { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY } public class EnumExample { Day day; public EnumExample(Day day) { this.day = day; } public void dayIsLike() { switch (day) { case MONDAY: System.out.println("Mondays are bad."); break; case FRIDAY: System.out.println("Fridays are better."); break; case SATURDAY: case SUNDAY: System.out.println("Weekends are best."); break; default: System.out.println("Midweek days are so-so."); break; } } public static void main(String[] args) { EnumExample firstDay = new EnumExample(Day.MONDAY); firstDay.dayIsLike(); // Output: Mondays are bad. EnumExample thirdDay = new EnumExample(Day.WEDNESDAY); thirdDay.dayIsLike(); // Output: Midweek days are so-so. } } |
Enum with Methods and Constructors
Enums in Java can have fields, constructors, and methods. Here’s an example of an enum representing directions, with a method to get the direction description:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
enum Direction { EAST("East"), WEST("West"), NORTH("North"), SOUTH("South"); private final String directionDescription; // Enum constructor Direction(String description) { this.directionDescription = description; } public String getDirectionDescription() { return directionDescription; } } public class DirectionExample { public static void main(String[] args) { Direction dir = Direction.NORTH; System.out.println(dir.getDirectionDescription()); // Output: North } } |
Iterating Over Enums
You can iterate over the values of an enum using the values()
method, which returns an array of enum constants.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
enum Season { WINTER, SPRING, SUMMER, FALL } public class SeasonExample { public static void main(String[] args) { for (Season season : Season.values()) { System.out.println(season); } // Output: // WINTER // SPRING // SUMMER // FALL } } |
Enum in a Switch Statement
Enums work seamlessly with switch statements, offering a type-safe way to execute different code blocks based on enum values.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
enum Level { HIGH, MEDIUM, LOW } public class SwitchExample { public static void main(String[] args) { Level level = Level.MEDIUM; switch (level) { case HIGH: System.out.println("High level"); break; case MEDIUM: System.out.println("Medium level"); break; case LOW: System.out.println("Low level"); break; } // Output: Medium level } } |
Enums with Abstract Methods
Enums can declare abstract methods, which allows each constant to provide its own implementation.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
enum Operation { ADD { public int execute(int x, int y) { return x + y; } }, SUBTRACT { public int execute(int x, int y) { return x - y; } }; public abstract int execute(int x, int y); } public class AbstractMethodExample { public static void main(String[] args) { System.out.println(Operation.ADD.execute(5, 3)); // Output: 8 System.out.println(Operation.SUBTRACT.execute(5, 3)); // Output: 2 } } |
Conclusion
Java enums offer a powerful mechanism to define a set of predefined constants, providing a type-safe way to work with fixed sets of values. With support for fields, methods, and constructors, enums can be as complex as required by your application, enhancing readability, maintainability, and safety of your code.