Fully qualified enum constants in switch labels since Java 21

Alex Buckley alex.buckley at oracle.com
Wed Nov 6 19:54:27 UTC 2024


On 11/6/2024 8:59 AM, Turkhan Badalov wrote:
> Since Java 21 using fully-qualified enum names in switch labels compiles 
> and works fine. Yet I can't find anything in JLS which would result in 
> this change. Is that a bug or intended to be so?

Allowing qualified enum constants as case constants was indeed part of 
Java 21. It came in via JEP 441: https://openjdk.org/jeps/441#History

I recommend looking at the evolution of JLS 14.11.1 in Gavin's spec 
drafts for JEP 441: https://cr.openjdk.org/~gbierman/jep440%2B441/

The drafts initially spelled out that "the (simple or qualified) name of 
an enum constant" is allowed, but later minimized change by reusing the 
phrase from JLS 20: "the name of an enum constant". That phrase is 
accurate: it allows all the names expressible in the grammar, which was 
changed previously to use ConditionalExpression and thus allows 
qualified as well as simple names.

Alex



More information about the amber-dev mailing list