I think it won’t work exactly like this unless you have access to a type variable (through either a type or method signature). The problem is the method signature of Enum.valueOf:
public static <T extends Enum<T>> T valueOf(
Class<T> enumType,
String name
);
There’s no way to get a T without a type variable. But you can do it like this if you’re willing to suppress some compiler warnings:
public enum King{
ELVIS
}
@SuppressWarnings({ "unchecked", "rawtypes" })
public static void main(final String[] args){
final Class<? extends Enum> enumType = King.class;
final Enum<?> theOneAndOnly = Enum.valueOf(enumType, "ELVIS");
System.out.println(theOneAndOnly.name());
}
Output:
ELVIS