How to Use Enum in If Condition in Java



This Java example demonstrates how to use Enum in if condition in Java.

public class EnumInIfStatement {

    public static void main(String[] args) {
        System.out.println(enumInIf(Days.SUNDAY));
    }
    enum Days {
        SUNDAY,
        MONDAY,
        TUESDAY,
        WEDNESDAY,
        THURSDAY,
        FRIDAY,
        SATURDAY;
    }

    public static String enumInIf(Days day) {
        if (day == Days.SUNDAY) {
            return "Its Sunday :-)";
        } else if (day == Days.MONDAY) {
            return "Its Monday :*--(";
        } else if (day == Days.TUESDAY) {
            return "Its Tuesday :*-(";
        } else if (day == Days.WEDNESDAY) {
            return "Its Wednesday :*(";
        } else if (day == Days.THURSDAY) {
            return "Its Thursday :)";
        } else if (day == Days.FRIDAY) {
            return "Its Friday ;-D";
        } else {
            return "Its Saturday :=D";
        }
    }
}
Output:
Its Sunday :-)

Comments