I want to get an instance to an enum type, so that:
String enumString="abc";
MyClass.MyEnum enumType=Class.forName("com.MyClass.MyEnum."+enumString);
This gives me an inconvertible types.
Enum.valueOf will do it, but it is pretty picky about it's type. Make sure you cast the Class
to Class<? extends Enum>
. Example:
enum Foo {
BLAT,
BLARG
};
System.out.println(Enum.valueOf((Class<? extends Enum>)Class.forName("Foo"), "BLARG"));
Have a look at Enum.valueOf( Class enumType, String name ).
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