1. Introduction
An enum (short for “enumeration”) is a special Java type used to define collections of constants. But Java enums are not just syntactic sugar—they’re full-fledged classes that extend java.lang.Enum
and offer compile-time type safety and additional functionality. In this article, you will learn how to create and use an enumeration in your Java applications.
2. Declaring Type-Safe Enums
Before Java 5, developers often used public static final
constants:
public class Color {
public static final int RED = 1;
public static final int GREEN = 2;
public static final int BLUE = 3;
}
This approach lacks type safety. Java enums solve this:
public enum Color {
RED, GREEN, BLUE;
}
Now Color.RED
is a constant of type Color
, and invalid values like 4
are no longer possible. This eliminates a whole class of bugs and improves code clarity.
The syntax to declare an enum is as follows:
access_modifier enum enum_type_name{
CONSTANT1, CONSTANT2…..
//fields, constructor, methods
}
3. Using Type-Safe Enums
Enums can be used like other objects.
Color c = Color.RED;
if (c == Color.RED) {
System.out.println("Color is red");
}
4. Declaring Enum Constructors and Members
Enums can have fields, methods, and constructors. Each constant can hold its own data:
public enum Day {
MONDAY("Weekday"),
TUESDAY("Weekday"),
WEDNESDAY("Weekday"),
THURSDAY("Weekday"),
FRIDAY("Weekday"),
SATURDAY("Weekend"),
SUNDAY("Weekend");
private final String type;
Day(String type) {
this.type = type;
}
public String getType() {
return type;
}
}
Usage:
System.out.println(Day.SATURDAY.getType()); // Weekend
5. Implicit Static Method for Enum Types
Every enum in Java has two built-in static methods:
values()
: Returns an array of enum constants in declared order.valueOf(String name)
: Returns the enum constant with the specified name.
Example:
Day d = Day.valueOf("MONDAY");
System.out.println(d); // MONDAY
for (Day day : Day.values()) {
System.out.println(day);
}
6. Inherited Methods from java.lang.Enum
Class
All enums implicitly extend java.lang.Enum
, and thus inherit several useful methods:
name()
– Returns the name of the constant as declared.ordinal()
– Returns the position of the constant in the enum.compareTo()
– Compares based on ordinal.equals()
andhashCode()
– Behave as expected for identity.toString()
– By default, returns the name; can be overridden.
Example:
System.out.println(Day.MONDAY.name()); // MONDAY
System.out.println(Day.SUNDAY.ordinal()); // 6
7. Implementing Interfaces
Enums can implement interfaces to define behavior:
interface Greetable {
void greet();
}
public enum Language implements Greetable {
ENGLISH {
public void greet() { System.out.println("Hello"); }
},
FRENCH {
public void greet() { System.out.println("Bonjour"); }
};
}
Usage:
Language.ENGLISH.greet(); // Hello
8. Enum in Switch Expressions
From Java 14 onwards, switch expressions with enums are more expressive. A switch expression is always exhaustive. This means that the cases defined in the switch expression must cover all values of the selector expression type. Otherwise, the code will not compile.
String action = switch (Day.SUNDAY) {
case MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY -> "Work";
case SATURDAY, SUNDAY -> "Relax";
};
System.out.println(action); // Relax
The arrow syntax (->
) and expression-style return (yield
in older versions) make switches more concise and less error-prone.
9. Conclusion
Enums in Java provide much more than a list of constants:
- They are type-safe, immutable, and extensible
- You can attach fields and methods, implement interfaces, and use them in modern switch expressions
- They make code more robust, readable, and easy to maintain
By using enums wisely, you can reduce bugs, improve clarity, and write cleaner object-oriented code.
You can find the complete code of this article here in GitHub.