Switch on Enum in Java

To use a switch statement on an enumeration (enum) in Java, you can use the enum type as the control expression for the switch statement. Here is an example of how to do this:

public enum Day {
    SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY
}

public class EnumSwitchExample {

    public static void main(String[] args) {
        Day day = Day.MONDAY;
        switch (day) {
            case SUNDAY:
                System.out.println("Sunday");
                break;
            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;
        }
    }

}

This example defines an enum called Day with the seven days of the week, and then uses a switch statement to print the name of the day.

Note that you must include a break statement at the end of each case block, or the code for all subsequent cases will also be executed.

I hope this helps. Let me know if you have any questions.