How come java doesn't allow the following generic return type:
public <T extends Enum<T> & MyInterface> Class<T> getEnum() {
return MyEnum.class;
}
While the following does work:
public <T extends Enum<T> & MyInterface> Class<T> getEnum(Class<T> t) {
return t;
}
getEnum(MyEnum.class);
MyEnum is an enumaration that implements the interface MyInterface.
Why am I not allowed to return MyEnum.class?
EDIT:
I need this because the function getEnum() is in an interface. It could be defined as follows:
@Override
public Class<MyEnum> getEnum() {
return MyEnum.class;
}
But what would then then be the return type of the interface method to allow any Class object of a class that is both an enum and implements MyInterface?
Your method is parameterized by T - the idea is that the caller gets to specify what T is - not the method implementation.
The call to the second method works because T is implicitly specified (by the caller) to be MyEnum.
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