Consider:
enum MyEnum {
A,
B { void foo() { } }
}
Then:
MyEnum.A.getClass().isEnum() ==> true
MyEnum.B.getClass().isEnum()) ==> false
This is correct and is as specified by JLS 8.9.1. However, it's counterintuitive, as one might reasonably expect enum constants to be instances of the enum class. This isn't the case, as enum constants with method bodies are instances of an anonymous subclass of the enum class, and isEnum() is [correctly] false for such subclasses.
This might be worth noting in the Class.isEnum() method doc.
enum MyEnum {
A,
B { void foo() { } }
}
Then:
MyEnum.A.getClass().isEnum() ==> true
MyEnum.B.getClass().isEnum()) ==> false
This is correct and is as specified by JLS 8.9.1. However, it's counterintuitive, as one might reasonably expect enum constants to be instances of the enum class. This isn't the case, as enum constants with method bodies are instances of an anonymous subclass of the enum class, and isEnum() is [correctly] false for such subclasses.
This might be worth noting in the Class.isEnum() method doc.
- relates to
-
JDK-4989735 (reflect) Class.isEnum should use new VM flag bit position
-
- Resolved
-
-
JDK-4990789 (reflect) Class.isEnum incorrect result
-
- Resolved
-
-
JDK-5020490 (reflect) Class.isEnum should return false for specialized enum constants
-
- Resolved
-
-
JDK-8234917 Explicitly discuss java.lang.Enum in Class.isEnum spec
-
- Resolved
-