How to Use Enum with Switch Statement in Java

This Java example shows how to use enums in switch statement in Java.

public class EnumInSwitchStatement {

    public static void main(String[] args) {
        System.out.println(enumInSwitch(Days.SUNDAY));
    }

    enum Days {
        SUNDAY,
        MONDAY,
        TUESDAY,
        WEDNESDAY,
        THURSDAY,
        FRIDAY,
        SATURDAY;
    }

    public static String enumInSwitch(Days day) {
        switch (day) {
            case SUNDAY:
                return "Its Sunday!!";
            case MONDAY:
                return "Its Monday";
            case TUESDAY:
                return "Its Tuesday";
            case WEDNESDAY:
                return "Its Wednesday";
            default:
                return "Rest of the week....";
        }
    }
}
Output:
Its Sunday!!

Comments