What is an enum?

An enum (enumeration) is a special type of Java class used to represent a group of constants. The enum class can be used to represent a fixed set of constants whose behavior and value does not change, such as the day of the week, the month of the year, and so on.

When to use enum?

Enums are very much useful when you have to keep static and final values in a program, the same thing can be achieved by creating a static final variable but enum is more specific and improves type safety. Enum is useful when you want to keep data like-

  1. Days of a week (SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY).
  2. Direction (NORTH, SOUTH, EAST, WEST).
  3. Gender (MALE, FEMALE, OTHER).
  4. Season (WINTER, SPRING, SUMMER, FALL).
  5. Level (LOW, MEDIUM, HIGH).
  6. etc…

How to create an enum?

Enums are similar to Java classes, but it has some special properties and you need to use the keyword ‘enum’ rather than ‘class’ to create them. Following is the syntax and simple example to create an enum.

Syntax

<modifier> enum <enum-name> {

    // Enum body
}

Example enum

An example enum that shows how you can use an enum to keep static direction value.

public enum Direction {

    NORTH,
    SOUTH,
    EAST,
    WEST
}

An enum can be used with if-else and switch case statement for conditional comparison. You can also pass an enum value in a method as a parameter but you are not allowed to create an object of an enum and cannot extend another enum however an enum can implement interfaces.

Enum with customized value

By default, enum has its own string value, but we can assign some customize values according to our requirement. We can map and store different types of data with a single enum type see the following example to better understand.

Example

public enum WeekDay {

    SUNDAY("Damn"),
    MONDAY("Hate"),
    TUESDAY("Annoy"),
    WEDNESDAY("Ignore"),
    THURSDAY("Smile"),
    FRIDAY("Love"),
    SATURDAY("Enjoy");

    private final String message;

    WeekDay(String message) {
        this.message = message;
    }

    public String getMessage() {
        return message;
    }

}

Driver class containing the main() method.

public class Main {

    public static void main(String[] args) {

        // Get all the values
        for (WeekDay weekDay : WeekDay.values()) {

            // Get the message of weekDay        
            System.out.println(weekDay.getMessage());
        }

    }
}

Output

>> javac Main.java
>> Java Main
Damn
Hate
Annoy
Ignore
Smile
Love
Enjoy

Points to remember

  1. You can not create an object of an enum.
  2. An enum cannot extend other classes.
  3. An enum can have data members and member functions just like other classes.
  4. Values are always fixed and read-only in an enum.
  5. It is a good practice to write an enum type in uppercase format.

Different examples

Following are the different examples of an enum, you can try and modify them to better understand.

Enum with if-else

The following example program shows how you can use an enum with the if-else statement.

public class EnumIfElse {

    // Weekday enum
    public enum WeekDay {

        SUNDAY,
        MONDAY,
        TUESDAY,
        WEDNESDAY,
        THURSDAY,
        FRIDAY,
        SATURDAY;

    }

    // Main method
    public static void main(String[] args) {

        for (WeekDay weekDay : WeekDay.values()) {

            // Enum in if-else
            if (weekDay == WeekDay.SATURDAY) {

                System.out.println("Begin the party!");
                break;

            }

        }

    }

}

Enum with switch-case

The following example program shows how you can use an enum with the switch-case statement.

public class EnumSwitchCase {

    // Weekday enum
    public enum WeekDay {

        SUNDAY,
        MONDAY,
        TUESDAY,
        WEDNESDAY,
        THURSDAY,
        FRIDAY,
        SATURDAY;

    }

    // Main method
    public static void main(String[] args) {

        // Input taken from user
        WeekDay input = WeekDay.FRIDAY;

        // Enum with switch-case
        switch (input) {

            case SUNDAY:
                System.out.println("Damn");
                break;

            case MONDAY:
                System.out.println("Hate");
                break;

            case TUESDAY:
                System.out.println("Annoy");
                break;

            case WEDNESDAY:
                System.out.println("Ignore");
                break;

            case THURSDAY:
                System.out.println("Smile");
                break;

            case FRIDAY:
                System.out.println("Love");
                break;

            case SATURDAY:
                System.out.println("Enjoy");
                break;

            default:
                System.out.println("Invalid!");
                break;
        }
    }

}