Java Enum Methods

Summary: in this tutorial, you will learn how to use Java enum methods to encapsulate behaviors related to enum constants.

Introduction to Java enum methods

In Java, an enum is a special class that extends the java.lang.Enum class:

Java Enum Methods

Like a regular class, you can define a method for an enum to encapsulate behavior related to enum constants.

Here’s the basic syntax for defining a method in an enum:

enum MyEnum {
    CONSTANTS;
    
    returnType methodName(parameterList) {
        // ..
    }
}Code language: Java (java)

Notice that you need to end the constant list with a semicolon (;) before defining the method.

Defining an enum method example

Suppose, you have an enum called Day that contains seven constants representing Sunday to Monday:

enum Day {
    MONDAY,
    TUESDAY,
    WEDNESDAY,
    THURSDAY,
    FRIDAY,
    SATURDAY,
    SUNDAY;
}Code language: Java (java)

The following shows how to define a method called isWeekend() that returns true if the constant is SATURDAY or SUNDAY, or false otherwise:

enum Day {
    MONDAY,
    TUESDAY,
    WEDNESDAY,
    THURSDAY,
    FRIDAY,
    SATURDAY,
    SUNDAY;

    public boolean isWeekend() {
        return this == SATURDAY || this == SUNDAY;
    }
}Code language: Java (java)

To use the isWeekend() method of the Day enum class, you call it via an enum constant. For example:

Day day = Day.SUNDAY;
if (day.isWeekend()) {
    System.out.println("It's a weekend.");
}Code language: Java (java)

Here’s the complete program:

public class App {
    public static void main(String[] args) {
        enum Day {
            MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY;

            public boolean isWeekend() {
                return this == SATURDAY || this == SUNDAY;
            }
        }

        Day day = Day.SUNDAY;
        if (day.isWeekend()) {
            System.out.println("It's a weekend.");
        }

    }
}Code language: Java (java)

Output:

It's a weekend.Code language: Java (java)

An enum can have more than one method. For example, the following adds a new method called getAbbreviation() to the Day enum class. The getAbbreviation() method returns the abbreviation of a day:

enum Day {
    MONDAY,
    TUESDAY,
    WEDNESDAY,
    THURSDAY,
    FRIDAY,
    SATURDAY,
    SUNDAY;

    public boolean isWeekend() {
        return this == SATURDAY || this == SUNDAY;
    }


    public String getAbbreviation() {
        return this.toString().substring(0, 3);
    }
}Code language: Java (java)

The following iterates over the constants of the Day enum and displays their abbreviations by calling the getAbbreviation() method:

public class App {
    public static void main(String[] args) {
        enum Day {
            MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY;

            public boolean isWeekend() {
                return this == SATURDAY || this == SUNDAY;
            }


            public String getAbbreviation() {
                return this.toString().substring(0, 3);
            }
        }


        for (var day : Day.values()) {
            System.out.print(day.getAbbreviation() + " ");
        }

    }
}Code language: Java (java)

Output:

MON TUE WED THU FRI SAT SUNCode language: Java (java)

Overriding toString() method

By default, the toString() method returns the constant of the enum. For example:

public class App {
    public static void main(String[] args) {
        enum Day {
            MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY;

            public boolean isWeekend() {
                return this == SATURDAY || this == SUNDAY;
            }


            public String getAbbreviation() {
                return this.toString().substring(0, 3);
            }
        }


        for (var day : Day.values()) {
            System.out.println(day);
        }

    }
}Code language: Java (java)

Output:

MONDAY
TUESDAY
WEDNESDAY
THURSDAY
FRIDAY
SATURDAY
SUNDAYCode language: Java (java)

It’s possible to override the toString() method to customize the string representation of an enum constant.

For example, the following illustrates how to override the toString() method of the Day enum class:

public class App {
    public static void main(String[] args) {
        enum Day {
            MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY;

            public boolean isWeekend() {
                return this == SATURDAY || this == SUNDAY;
            }


            public String getAbbreviation() {
                return this.toString().substring(0, 3);
            }
            
            @Override
            public String toString() {
                return String.format("Day(value=%s)", super.toString());
            }
        }


        for (var day : Day.values()) {
            System.out.println(day);
        }

    }
}Code language: Java (java)

Output:

Day(value=MONDAY)
Day(value=TUESDAY)
Day(value=WEDNESDAY)
Day(value=THURSDAY)
Day(value=FRIDAY)
Day(value=SATURDAY)
Day(value=SUNDAY)Code language: Java (java)

Summary

  • Use Java enum methods to encapsulate the behaviors related to enum constants.