When I specify an enum like this:
enum Color { RED }
The generated code javap -p Color looks like this:
final class Color extends java.lang.Enum<Color> {
public static final Color RED;
private static final Color[] $VALUES;
public static Color[] values();
public static Color valueOf(java.lang.String);
private Color();
private static Color[] $values();
static {};
}
However, when I add custom extensions like an explicit constructor, e.g.:
enum Color {
RED,
Color() {}
}
I get this:
class Color extends java.lang.Enum<Color> {
public static final Color RED;
public static final Color Color;
private static final Color[] $VALUES;
public static Color[] values();
public static Color valueOf(java.lang.String);
private Color();
private static Color[] $values();
static {};
}
Missing the final modifier for generated class Color. However, I cannot extend Color (as usual):
final class B extends Color {}
The compiler gives:
error: enum types are not extensible
final class B extends Color {}
^
1 error
So why is the final modifier missing when adding custom code to the enum class? I suppose the prevention of subclassing Color anyway is performed by a sanity check from the compiler (aka: if super class typeof enum => deny), right?
I am using OpenJDK 17.0.2