Possible Duplicate:
Why do I get an Enum constant reference cannot be qualified in a case label?
Hi, Does someone knows why when I switch over an Enum, the cases should be on the unqualified Enum value?
Example:
switch(var) {
case Enum.FIRST:
break;
}
is illegal
but:
switch(var) {
case FIRST:
break;
}
is legal.
I understand that var is of a specific type (Enum) but why the compiler cares if I use the fully qualified name of the Enum value?
Because the Java Language Specification states that it is so.
Specifically, the defintition of a SwitchLabel:
SwitchLabel:
case ConstantExpression :
case EnumConstantName :
default :
See http://java.sun.com/docs/books/jls/third_edition/html/statements.html#258896
I'm guessing because otherwise you could do something like this:
switch(var) {
case AnyOtherEnum.FIRST:
break;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With