Community
Participate
Working Groups
Try to compile these two classes using javac. 1) package p; public enum A { CONST1, CONST2, CONST3 } 2) package p; public class B { public static void main(String[] args) { for (A a : A.values()) System.out.println(foo(a)); } private static String foo(A a) { switch (a) { case A.CONST1: return "CONST1"; case A.CONST2: return "CONST2"; case A.CONST3: return "CONST3"; } return "DOESN'T EXIST"; } } The compilation of B returns: p/B.java:11: an enum switch case label must be the unqualified name of an enumeration constant case A.CONST1: ^ p/B.java:13: an enum switch case label must be the unqualified name of an enumeration constant case A.CONST2: ^ p/B.java:13: duplicate case label case A.CONST2: ^ p/B.java:15: an enum switch case label must be the unqualified name of an enumeration constant case A.CONST3: ^ p/B.java:15: duplicate case label case A.CONST3: ^ 5 errors You need to replace B.java with: package p; import p.A.*; public class B { public static void main(String[] args) { for (A a : A.values()) System.out.println(foo(a)); } private static String foo(A a) { switch (a) { case CONST1: return "CONST1"; case CONST2: return "CONST2"; case CONST3: return "CONST3"; } return "DOESN'T EXIST"; } } and then it compiles fine. Looking at the specs they changed the specs to say that a case label can be: - constant expression (doesn't include enum types) - enum constant where enum constant is an identifier. So we need to check that the name reference in a switch statement is a single name reference if it resolves to a enum type.
Created attachment 15425 [details] Apply on HEAD
Integrating patch. Added regression test: EnumTest#test022. Fixed
Verified for 3.1M3 with build I200411040100