Enumerated data types, or enums for short, are a built-in feature in Java and provide a convenient way of representing a set of predefined constants. They make your code more readable, maintainable, and efficient, and they can also help prevent bugs caused by invalid data.
Table of Contents
How to Declare a Java Enum?
Declaring a Java Enum is easy. Here is the syntax for declaring an Enum:
enum EnumName {
CONSTANT1,
CONSTANT2,
...
CONSTANTn;
}
For example, let's declare an enum for the days of the week:
enum DaysOfWeek {
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY,
SUNDAY;
}
Java Enum Methods
There are several methods available for working with Java enums, including values()ordinal(), and valueOf().
- values() returns an array of all the constants in the enum.
- ordinal() returns the position of the constant in the enum.
- valueOf() returns the constant in the enum that matches the string passed as an argument.
Here’s an example that demonstrates the use of these methods:
enum DaysOfWeek {
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY,
SUNDAY;
}
public class Main {
public static void main(String[] args) {
DaysOfWeek[] days = DaysOfWeek.values();
for (DaysOfWeek day : days) {
System.out.println(day + ": " + day.ordinal());
}
DaysOfWeek day = DaysOfWeek.valueOf("FRIDAY");
System.out.println("valueOf: " + day);
}
}
Output

Java Enum Constructors
Java enums can have constructors, and these constructors can be used to pass arguments to the enum constants.Enum objects cannot be instantiated directly, so their constructor can not be called.
Here’s an example of an enum with a constructor:
enum Color {
RED("#FF0000"),
GREEN("#00FF00"),
BLUE("#0000FF");
private final String hexCode;
Color(String hexCode) {
this.hexCode = hexCode;
}
public String getHexCode() {
return hexCode;
}
}
public class Main {
public static void main(String[] args) {
for (Color color : Color.values()) {
System.out.println(color + ": " + color.getHexCode());
}
}
}
Output

Java Enum with Switch Statement
The switch statement allows you to take different actions based on the value of an expression. With enums, you can write more readable and maintainable switch statements, since you are working with a predefined set of constants.
Here’s an example of a switch statement that uses the DaysOfWeek enum from the previous section:
public class Main {
public static void main(String[] args) {
DaysOfWeek day = DaysOfWeek.MONDAY;
switch (day) {
case MONDAY:
System.out.println("Monday");
break;
case TUESDAY:
System.out.println("Tuesday");
break;
case WEDNESDAY:
System.out.println("Wednesday");
break;
case THURSDAY:
System.out.println("Thursday");
break;
case FRIDAY:
System.out.println("Friday");
break;
case SATURDAY:
System.out.println("Saturday");
break;
case SUNDAY:
System.out.println("Sunday");
break;
}
}
}
Output

Java Enum and Inheritance
Java enums are a special data type that can extend the java.lang.Enum class, which makes them final and cannot be further subclassed. This helps maintain the integrity of the set of predefined constants. However, enums can still implement interfaces.
Here’s an example of an enum that implements an interface:
interface Day {
void display();
}
enum DaysOfWeek implements Day {
MONDAY("Monday") {
public void display() {
System.out.println("Monday");
}
},
TUESDAY("Tuesday") {
public void display() {
System.out.println("Tuesday");
}
},
WEDNESDAY("Wednesday") {
public void display() {
System.out.println("Wednesday");
}
},
THURSDAY("Thursday") {
public void display() {
System.out.println("Thursday");
}
},
FRIDAY("Friday") {
public void display() {
System.out.println("Friday");
}
},
SATURDAY("Saturday") {
public void display() {
System.out.println("Saturday");
}
},
SUNDAY("Sunday") {
public void display() {
System.out.println("Sunday");
}
};
private String displayName;
DaysOfWeek(String displayName) {
this.displayName = displayName;
}
public String getDisplayName() {
return displayName;
}
}
public class Main {
public static void main(String[] args) {
DaysOfWeek[] days = DaysOfWeek.values();
for (DaysOfWeek day : days) {
day.display();
}
}
}
Output

Java Enum and Abstract Methods
In Java, enums can contain abstract methods, which can be overridden by each constant in the enum. This allows you to define a common behavior for all constants, but also provide specific implementation for each one.
Here’s an example of an enum with an abstract method:
interface Day {
void display();
}
enum DaysOfWeek implements Day {
MONDAY("Monday") {
public void display() {
System.out.println("Monday");
}
},
TUESDAY("Tuesday") {
public void display() {
System.out.println("Tuesday");
}
},
WEDNESDAY("Wednesday") {
public void display() {
System.out.println("Wednesday");
}
},
THURSDAY("Thursday") {
public void display() {
System.out.println("Thursday");
}
},
FRIDAY("Friday") {
public void display() {
System.out.println("Friday");
}
},
SATURDAY("Saturday") {
public void display() {
System.out.println("Saturday");
}
},
SUNDAY("Sunday") {
public void display() {
System.out.println("Sunday");
}
};
private String displayName;
DaysOfWeek(String displayName) {
this.displayName = displayName;
}
public String getDisplayName() {
return displayName;
}
}
public class Main {
public static void main(String[] args) {
DaysOfWeek[] days = DaysOfWeek.values();
for (DaysOfWeek day : days) {
day.display();
}
}
}
Output

Conclusion
Java enums are a data type that provide benefits over traditional constants by allowing for improved readability and maintainability, as well as methods and abstract methods. They are essential for writing clear, concise, and maintainable code in Java.