As of Java 16, we can now define an enum locally, within a method. This feature came as part of the work for the new records feature. (Interfaces too can be local, by the way.)
What is the fully-qualified name of such a locally-defined enum class?
The following code works if I were define the enum in a separate .java
file. How should I change this fully-qualified class name for a local enum?
package work.basil.example.enums;
public class EnumMagic
{
public static void main ( String[] args )
{
EnumMagic app = new EnumMagic ( );
app.demo ( );
}
private void demo ( )
{
enum Shape { CIRCLE, SQUARE }
try
{
Class < ? > clazz = Class.forName ( "work.basil.example.enums.Shape" );
System.out.println ( "clazz = " + clazz );
}
catch ( ClassNotFoundException e ) { throw new RuntimeException ( e ); }
}
}
As currently written, I understandably get an java.lang.ClassNotFoundException: work.basil.example.enums.Shape
.
Neither of these work:
"work.basil.example.enums.EnumMagic.Shape"
"work.basil.example.enums.EnumMagic.$Shape"