Java Enumerations
Introduction
Have you ever needed to represent a fixed set of constants in your Java program, such as days of the week, months, or card suits? Before Java 5, developers used static final constants for this purpose, which had several limitations. Java Enumerations (or enum
types) were introduced in Java 5 to solve these problems by providing a type-safe way to define collections of constants.
In this lesson, you'll learn:
- What enumerations are and why they're useful
- How to create and use basic enums
- Advanced enum features like methods, fields, and constructors
- Best practices for working with enumerations
- Real-world applications of enums
What Are Java Enumerations?
An enumeration (or enum
) is a special type of class that represents a group of constants (unchangeable variables). Enumerations serve the purpose of representing fixed sets of values that a variable can take.
Basic Enum Creation and Usage
Let's start with a simple example of creating and using an enum:
// Define an enum for days of the week
public enum Day {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}
Here's how to use this enum:
// Using the Day enum
public class EnumDemo {
public static void main(String[] args) {
// Declare a Day variable
Day today = Day.WEDNESDAY;
// Use in conditional statements
if (today == Day.WEDNESDAY) {
System.out.println("It's midweek!");
}
// Switch with enum
switch (today) {
case MONDAY:
System.out.println("Start of work week");
break;
case FRIDAY:
System.out.println("End of work week");
break;
case SATURDAY:
case SUNDAY:
System.out.println("Weekend!");
break;
default:
System.out.println("Midweek");
break;
}
}
}
Output:
It's midweek!
Midweek
Enum Methods and Properties
Java automatically adds some useful methods to all enums:
Built-in Methods
values()
- Returns an array of all enum constantsvalueOf(String name)
- Returns the enum constant with the specified namename()
- Returns the name of the enum constantordinal()
- Returns the position of the enum constant (0-based)
public class EnumMethodsDemo {
public static void main(String[] args) {
// Using values() to iterate through all enum constants
System.out.println("All days of the week:");
for (Day day : Day.values()) {
System.out.println(day);
}
// Using valueOf()
Day specificDay = Day.valueOf("MONDAY");
System.out.println("Specific day: " + specificDay);
// Using name() and ordinal()
System.out.println("Name: " + Day.FRIDAY.name());
System.out.println("Ordinal value: " + Day.FRIDAY.ordinal());
}
}
Output:
All days of the week:
MONDAY
TUESDAY
WEDNESDAY
THURSDAY
FRIDAY
SATURDAY
SUNDAY
Specific day: MONDAY
Name: FRIDAY
Ordinal value: 4
Adding Fields, Constructors, and Methods to Enums
Enums can be much more powerful than simple constants. They can have:
- Fields
- Constructors
- Methods
Let's expand our previous example:
public enum Month {
// Enum constants with values (calls constructor)
JANUARY(31, "Winter"),
FEBRUARY(28, "Winter"),
MARCH(31, "Spring"),
APRIL(30, "Spring"),
MAY(31, "Spring"),
JUNE(30, "Summer"),
JULY(31, "Summer"),
AUGUST(31, "Summer"),
SEPTEMBER(30, "Fall"),
OCTOBER(31, "Fall"),
NOVEMBER(30, "Fall"),
DECEMBER(31, "Winter");
// Fields
private final int days;
private final String season;
// Constructor
Month(int days, String season) {
this.days = days;
this.season = season;
}
// Getter methods
public int getDays() {
return days;
}
public String getSeason() {
return season;
}
// Custom method
public boolean isLongMonth() {
return days == 31;
}
}
Now let's use our enhanced enum:
public class EnhancedEnumDemo {
public static void main(String[] args) {
Month myMonth = Month.APRIL;
System.out.println(myMonth + " has " + myMonth.getDays() + " days");
System.out.println(myMonth + " is in the " + myMonth.getSeason() + " season");
if (myMonth.isLongMonth()) {
System.out.println(myMonth + " is a long month");
} else {
System.out.println(myMonth + " is a short month");
}
// Checking which months are in winter
System.out.println("\nWinter months:");
for (Month month : Month.values()) {
if (month.getSeason().equals("Winter")) {
System.out.println(month);
}
}
}
}
Output:
APRIL has 30 days
APRIL is in the Spring season
APRIL is a short month
Winter months:
JANUARY
FEBRUARY
DECEMBER
Implementing Interfaces with Enums
Enums can also implement interfaces, which adds another dimension of flexibility:
// Define an interface
interface Describable {
String getDescription();
}
// Enum implementing the interface
public enum TrafficLight implements Describable {
RED("Stop"),
YELLOW("Caution"),
GREEN("Go");
private final String action;
TrafficLight(String action) {
this.action = action;
}
// Implementing the interface method
@Override
public String getDescription() {
return name() + " means " + action;
}
// Additional method
public boolean shouldStop() {
return this == RED || this == YELLOW;
}
}
Using the enum that implements an interface:
public class TrafficLightDemo {
public static void main(String[] args) {
TrafficLight light = TrafficLight.YELLOW;
// Using interface method
System.out.println(light.getDescription());
// Using enum method
if (light.shouldStop()) {
System.out.println("You should stop at this light!");
} else {
System.out.println("You can proceed");
}
}
}
Output:
YELLOW means Caution
You should stop at this light!
EnumSet and EnumMap
Java provides special collection implementations optimized for use with enums:
EnumSet
EnumSet
is a specialized Set implementation for use with enum types:
import java.util.EnumSet;
public class EnumSetDemo {
public static void main(String[] args) {
// Create an empty EnumSet
EnumSet<Day> weekdays = EnumSet.noneOf(Day.class);
// Add elements
weekdays.add(Day.MONDAY);
weekdays.add(Day.TUESDAY);
weekdays.add(Day.WEDNESDAY);
weekdays.add(Day.THURSDAY);
weekdays.add(Day.FRIDAY);
System.out.println("Weekdays: " + weekdays);
// Create EnumSet with all values
EnumSet<Day> allDays = EnumSet.allOf(Day.class);
System.out.println("All days: " + allDays);
// Create EnumSet with a range
EnumSet<Day> weekend = EnumSet.range(Day.SATURDAY, Day.SUNDAY);
System.out.println("Weekend: " + weekend);
}
}
Output:
Weekdays: [MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY]
All days: [MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY]
Weekend: [SATURDAY, SUNDAY]
EnumMap
EnumMap
is a specialized Map implementation for use with enum keys:
import java.util.EnumMap;
public class EnumMapDemo {
public static void main(String[] args) {
// Create an EnumMap
EnumMap<Day, String> dayActivities = new EnumMap<>(Day.class);
// Add elements
dayActivities.put(Day.MONDAY, "Start the week");
dayActivities.put(Day.WEDNESDAY, "Midweek meeting");
dayActivities.put(Day.FRIDAY, "Plan for weekend");
dayActivities.put(Day.SATURDAY, "Relax and have fun");
// Access elements
System.out.println("Friday's activity: " + dayActivities.get(Day.FRIDAY));
// Iterate through EnumMap
System.out.println("\nWeek plan:");
for (Day day : Day.values()) {
String activity = dayActivities.getOrDefault(day, "No specific plans");
System.out.println(day + ": " + activity);
}
}
}
Output:
Friday's activity: Plan for weekend
Week plan:
MONDAY: Start the week
TUESDAY: No specific plans
WEDNESDAY: Midweek meeting
THURSDAY: No specific plans
FRIDAY: Plan for weekend
SATURDAY: Relax and have fun
SUNDAY: No specific plans