Friday, March 14, 2014

Java Enum In Switch Statements

How to use Java Enums in Switch statements;  use Java 1.7 and up.

/**
 *
 * @author h@ppyF@ce
 */
public class LogicClass {

    enum Month {

        JANUARY(1),
        FEBRUARY(2);

        private final int monthCode;

        Month(int monthCode) {
            this.monthCode = monthCode;
        }

        public int getMonthCode() {
            return monthCode;
        }

    };

    public static void main(String... args) {
        Month ci = Month.FEBRUARY;
        switch (ci) {
            case JANUARY:
                System.out.println("1st");
                break;
            case FEBRUARY:
                System.out.println("2nd");
                break;
            default:
                System.out.println("default");
        }
    }
}

No comments:

Post a Comment