I tried this:
public static enum Types { A, B, C }
Class<Enum> e = Types.class;
But I get an "incompatible types" error:
found : java.lang.Class<id.Types>
required: java.lang.Class<java.lang.Enum>
Class<Enum> e = Types.class;
As far as I know all enums inherit from Enum. Why is my enum incompatible to Enum?
Why not just Class<? extends Enum> e = Types.class;
?
UPD: I'll give you more explained answer, why your code does not work.
First of all, the type of expression Types.class
is Class<Types>
, and your variable e
is Class<Enum>
.
According to JLS 5.5.1 such types (i.e. Class<Types>
and Class<Enum>
) are provably distinct types (JLS 4.5), and their erasures are same (just Class
), so in this case it is compile-time error when you try to cast from Class<Types>
to Class<Enum>
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With