Java Reference
In-Depth Information
30. case Console.WII : //not valid
31. System.out.println(“WII console”);
32. break;
33. }
Line 27 attempts to use the ordinal value of XBOX , which is not allowed. Line 30 uses the
fully qualifi ed name of the WII element, which is also not allowed. The following compiler
errors occur:
EnumSwitch.java:27: an enum switch case label must be the unqualified
name of an enumeration constant
case 0 :
^
EnumSwitch.java:30: an enum switch case label must be the unqualified
name of an enumeration constant
case Console.WII :
^
Final case Values
A case value must be a constant expression. The examples in this chapter have been either
literals or enum constants, but you can also use final variables. Examine the following
code and try to determine its output:
public class FinalSwitch {
public static final char UPPER_A = 'A';
public static final char UPPER_B = 'B';
public static final char UPPER_C = 'C';
public static String convertGrade(char grade) {
String response = “”;
switch(grade) {
case UPPER_A :
case UPPER_B :
System.out.println(“Nice job!”);
break;
case UPPER_C :
System.out.println(“Not bad.”);
break;
default :
System.out.println(“Not good.”);
}
Search WWH ::




Custom Search