Community
Participate
Working Groups
Hello, I've found a bug in the processing of the enumerated types in JDK5.0 in Eclipse 3.1M4. let the following definition : enum EnumType {ONE, TWO, THREE); The following expression is currently impossible : EnumType currentType=EnumType.ONE; switch (currentType) { case EnumType.ONE: break; case EnumType.TWO: break; case EnumType.THREE: break; default: break; }
Following code is illegal, since enum constants cannot be qualified in switch cases. You should remove qualification, and it would still resolve as they are resolved relatively to the switch expression type (EnumType here). Also note that javac rejects this code, slightly wrong though (false duplicate case labels): >javac X.java -d ..\bin -Xlint X.java:7: an enum switch case label must be the unqualified name of an enumeration constant case EnumType.ONE: ^ X.java:10: an enum switch case label must be the unqualified name of an enumeration constant case EnumType.TWO: ^ X.java:10: duplicate case label case EnumType.TWO: ^ X.java:13: an enum switch case label must be the unqualified name of an enumeration constant case EnumType.THREE: ^ X.java:13: duplicate case label case EnumType.THREE: ^ 5 errors public class X { enum EnumType {ONE, TWO, THREE} public static void main(String[] args) { EnumType currentType=EnumType.ONE; switch (currentType) { case EnumType.ONE: break; case EnumType.TWO: break; case EnumType.THREE: break; default: break; } } }
OK Philippe ;) Sun has not well done its job with enumeration. No switch for an enumeration is silly imo :( thanks regards Xavier
You can switch on enum constants no problem. You cannot though qualify enum constant references *only* in case labels (they may re-enable them later on). You may still use static imports to grant access to them, or simply rely on the fact that they are implicitly resolved relatively to the switch expression (so not need to qualify individual constants); in your example, simply remove the qualification and it will compile clean.