Java switch statement with the enumeration Enum

First, use

Do not use an enumerated type in case statement to refer directly to the enum constant name.

Second, the principle analysis

Java books and reading official documents of the time, say Java Switch statements of support enumeration, but to find relevant instances when they did not find on the Internet. By looking at the official documentation, understand usage.
It is one kind of enumeration constants static final, but when the switch usually refer to the static constant use and there are differences. Because the switch conditions enumerated types must be the same case statement inside, so when declared enumerated types in a switch statement, case statement enumerated types have been identified, it is unnecessary to refer to the use of enumeration class .

Third, the sample code

public enum Day {
    SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
    THURSDAY, FRIDAY, SATURDAY 
}
 {class EnumTest public 
    Day Day;     
    public EnumTest (Day Day) = {Day this.day; 
    }     
    public void tellItLikeItIs () {Switch (Day) {Case MONDAY: // Switch conditional statements enumerated types have been identified, do not need to use an enumerated type enumeration constants to refer to, or compiler errors 
                System.out.println ( "Mondays are Bad."); BREAK;                     
            Case FRIDAY: 
                System.out.println (. "Fridays are of Better"); BREAK;                          
            Case SATURDAY : Case SUNDAY: 
                System.out.println (. "Weekends are Best"); BREAK;                          
            default:
                System.out.println (. "Days are Midweek SO-SO"); BREAK;
        }
    }    
    public static void main(String[] args) {
        EnumTest firstDay = new EnumTest(Day.MONDAY);
        firstDay.tellItLikeItIs();
        EnumTest thirdDay = new EnumTest(Day.WEDNESDAY);
        thirdDay.tellItLikeItIs();
        EnumTest fifthDay = new EnumTest(Day.FRIDAY);
        fifthDay.tellItLikeItIs();
        EnumTest sixthDay = new EnumTest(Day.SATURDAY);
        sixthDay.tellItLikeItIs();
        EnumTest seventhDay = new EnumTest(Day.SUNDAY);
        seventhDay.tellItLikeItIs();
    }
}

         

                   

Guess you like

Origin blog.51cto.com/14028890/2404662