In Java, enumerations (enums) are a special type used to define a group of named constants.
- Enums help in readability, maintainability, and type safety in programs by assigning meaningful names to integer values.
- Mainly useful when we have a small set of possible values for an item like directions, days of week, etc.
Creating an Enum
Syntax:
enum EnumName {
CONSTANT1, CONSTANT2, CONSTANT3;
}
Java enum TrafficLight { RED, GREEN, YELLOW; } public class Test{ public static void main(String[] args){ TrafficLight x = TrafficLight.RED; System.out.println(x); } } Declaration of enum in Java
Enum declaration can be done outside a class or inside a class but not inside a method.
1. Declaration outside the class
As we have seen in the above example, enums can be declared outside a class and accessed directly
2. Declaration inside a class
Enums can also be declared inside a class but not inside a method.
Java public class Test { enum Color { RED, GREEN, BLUE; } // Driver method public static void main(String[] args) { Color c1 = Color.RED; System.out.println(c1); } } Properties of Enum in Java
There are certain properties followed by Enum as mentioned below:
- Class Type: Internally implemented as a class.
- Enum Constants: Each constant is an object of the enum type.
- Switch Support: Can be used in switch statements.
- Implicit Modifiers: Constants are public static final.
- Inheritance: Enums cannot extend classes but can implement interfaces.
Enum in a Switch Statement
Enums can be used in switch statements to handle different cases based on the enum constants.
Java import java.io.*; class GFG{ // Enum Declared enum Color { RED, GREEN, BLUE, YELLOW; } public static void main(String[] args){ Color var_1 = Color.YELLOW; // Switch case with Enum switch (var_1) { case RED: System.out.println("Red color"); break; case GREEN: System.out.println("Green color"); break; case BLUE: System.out.println("Blue color"); break; default: System.out.println("Other color"); } } } Enum with Methods and Constructor
Enums can have constructors and methods, executed separately for each constant
Java enum Color{ RED, GREEN, BLUE; private Color(){ System.out.println("Constructor called for: " + this); } public void display(){ System.out.println("Color is: " + this); } } public class Test{ public static void main(String[] args){ Color c1 = Color.RED; c1.display(); } } OutputConstructor called for: RED Constructor called for: GREEN Constructor called for: BLUE Color is: RED
Enum with Abstract Methods
Enums can declare abstract methods that each constant must implement.
Java enum Day{ MONDAY{ public String getNumber(){ return "1st day"; } }, FRIDAY{ public String getNumber(){ return "5th day"; } }; public abstract String getNumber(); } public class EnumTest { public static void main(String[] args){ System.out.println(Day.MONDAY.getNumber()); System.out.println(Day.FRIDAY.getNumber()); } } Iterating Enums Using values()
Use EnumType.values() to loop through all enum constants.
Java enum Color{ RED, GREEN, BLUE; } public class Test{ public static void main(String[] args){ for (Color c : Color.values()){ System.out.println(c); } } } Enum and EnumSet (Specific Range Iteration)
EnumSet.range() allows iteration over a specific range of enum constants.
Java import java.util.EnumSet; public class EnumSetExample{ enum Day{ SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY; } public static void main(String[] args){ EnumSet<Day> workDays = EnumSet.range(Day.TUESDAY, Day.FRIDAY); for (Day d : workDays){ System.out.println(d); } } } OutputTUESDAY WEDNESDAY THURSDAY FRIDAY
Creating a Class with an Enum Member
We can combine enums with regular classes to organize your program logic. An enum can be a member variable in a class, and methods can perform actions based on the enum value.
Java /*package whatever //do not write package name here */ import java.io.*; enum Day { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY } public class EnumTest { // Enum member variable Day day; // constructor which takes enum value public EnumTest(Day day) { this.day = day; } // method to execute action as per enum value public void tellItLikeItIs() { switch (day) { case MONDAY: System.out.println("Mondays are tough"); break; case TUESDAY: System.out.println("Tuesday are better"); break; case WEDNESDAY: System.out.println("Wednesday are okay"); break; case THURSDAY: System.out.println("Thursdays are hopeful"); break; case FRIDAY: System.out.println("Fridays are exciting"); break; case SATURDAY: System.out.println("Saturdays are relaxing"); break; case SUNDAY: System.out.println("Sunday are for rest"); break; default: System.out.println("Everyday are good"); break; } } public static void main(String[] args) { EnumTest firstDay = new EnumTest(Day.MONDAY); firstDay.tellItLikeItIs(); EnumTest thirdDay = new EnumTest(Day.WEDNESDAY); thirdDay.tellItLikeItIs(); EnumTest fifthDay = new EnumTest(Day.FRIDAY); fifthDay.tellItLikeItIs(); EnumTest sixthDay = new EnumTest(Day.SATURDAY); sixthDay.tellItLikeItIs(); EnumTest seventhDay = new EnumTest(Day.SUNDAY); seventhDay.tellItLikeItIs(); } } OutputMondays are tough Wednesday are okay Fridays are exciting Saturdays are relaxing Sunday are for rest
Explanation:
- The EnumTest class in above code is created with member of type Day. It has constructor which takes Day enum as an argument and assigns it.
- The class has method tellItLikeItIs(), which prints message based on value of day.
- The main method includes objects of EnumTest using different Day enum values and calling tellItLikeItIs() method on each.
NOTE: The new keyword is used because EnumTest is a regular class, not an enum, so we create instances and pass the enum value to its constructor.
Explore
Java Basics
OOP & Interfaces
Collections
Exception Handling
Java Advanced
Practice Java