<div dir="ltr"><div>The JLS <a href="https://docs.oracle.com/javase/specs/jls/se21/html/jls-14.html#jls-14.11.1" target="_blank">says</a>
"Every case constant must be either a constant expression (§15.29), or
the name of an enum constant (§8.9.1), otherwise a compile-time error
occurs." which I believe explains why using anything besides simple name
of an enum constant in switch labels leads to compilation error. In
fact, this was very obvious prior to java 14 where SwitchLabel expected
EnumConstantName which itself was just an Identifier: <a href="https://docs.oracle.com/javase/specs/jls/se13/preview/switch-expressions.html#jep354-14.11.1" target="_blank">https://docs.oracle.com/javase/specs/jls/se13/preview/switch-expressions.html#jep354-14.11.1</a>
. Today, even though SwitchLabel expects a ConditionalExpression which
many layers down is ExpressionName, the restriction remained until Java
21.<br></div><div><br></div><div>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? Will appreciate if someone can point me to the part
of JLS that resulted in this change. Attaching a minimal reproducible
code which fails on Java versions prior to 21 and compiles since 21.<br></div><div><br></div><div><br></div><div>package org.example;<br><br>enum Demo {<br> ONE, TWO<br>}<br><br>public class Main {<br> public static void main(String[] args) {<br> final Demo input = Demo.ONE;<br> switch(input) {<br> case Demo.ONE: {}<br> case org.example.Demo.TWO: {}<br> }<br> }<br>}<br><br></div><div>Regards,<br></div><div>Turkhan</div></div>