26

(note: edited question; the prior intent was not clear)

Consider this code:

public final class Foo { private enum X { VALUE1, VALUE2 } public static void main(final String... args) { final X x = X.VALUE1; switch (x) { case VALUE1: System.out.println(1); break; case VALUE2: System.out.println(2); } } } 

This code works fine.

However, if I replace:

case VALUE1: // or VALUE2 

with:

case X.VALUE1: // or X.VALUE2 

then the compiler complains:

java: /path/to/Foo.java:whatever: an enum switch case label must be the unqualified name of an enumeration constant

SO suggests an answer with this quote from the JLS:

(One reason for requiring inlining of constants is that switch statements require constants on each case, and no two such constant values may be the same. The compiler checks for duplicate constant values in a switch statement at compile time; the class file format does not do symbolic linkage of case values.)

But this does not satisfy me. As far as I am concerned, VALUE1 and X.VALUE1 are exactly the same. The quoted text does not explain it at all for me.

Where in the JLS is it defined that enum values in switch statements have to be written this way?

12
  • 6
    It's simply the way it is because that's how the Java language spec says it is. What is it about the suggested answer you linked that you find lacking? Commented Jul 20, 2013 at 17:41
  • 1
    Simply because as far as I am concerned, VALUE1 or X.VALUE1 are both the same constant; I see no reason why I cannot substitute the latter for the former. Commented Jul 20, 2013 at 17:44
  • 1
    I suspect it's just simplicity - and after all, every case in any specific switch statement will have to be of the same type. Why do you want the extra flexibility (which would make the JLS more complicated)? Commented Jul 20, 2013 at 17:46
  • 1
    Well I suppose that class identifier in X.VALUE1, it is not and cannot be a compiletime constant , which is required by the switch statement, which is what the JLS entry says in a way Commented Jul 20, 2013 at 17:48
  • 3
    @fge: Please be clearer about this in the future then. There's a big difference between "show me where the language spec defines this" and "I don't understand why the language was designed this way" and both Brian and I read your question in the latter spirit. Commented Jul 20, 2013 at 18:02

1 Answer 1

10

SwitchLabel expects an EnumConstantName, which is defined as the enum constant identifier, which is unqualified:

EnumConstant:
Annotationsopt Identifier Argumentsopt ClassBodyopt

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a LOT! I have stumbled upon these two points of the JLS without being able to find the link between them
Since java 14 SwitchLabel expects CaseConstant which is ConditionalExpression. Link: docs.oracle.com/javase/specs/jls/se14/html/…. Could anyone please share where this is specified in JLS now? For what it's worth, java 21 compiles fine with fully qualified enum in case labels. So, I am a bit puzzled right now

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.