Reputation: 24767
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.
Upvotes: 7
Views: 15961
Reputation: 66069
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"));
Upvotes: 24
Reputation: 91881
You are looking for MyClass.MyEnum.valueOf(enumString)
. No need to fully qualify the class in the string.
Upvotes: 8