Summary: | JDK5.0 enum bug | ||
---|---|---|---|
Product: | [Eclipse Project] JDT | Reporter: | Xavier Méhaut <xavier.mehaut> |
Component: | Core | Assignee: | JDT-Core-Inbox <jdt-core-inbox> |
Status: | RESOLVED INVALID | QA Contact: | |
Severity: | normal | ||
Priority: | P3 | ||
Version: | 3.1 | ||
Target Milestone: | 3.1 M5 | ||
Hardware: | PC | ||
OS: | Windows XP | ||
Whiteboard: |
Description
Xavier Méhaut
2004-12-22 04:00:38 EST
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. |