See Class.forName(String).
Returns the Class object associated with the class or interface with the given string name.
Do not forget to put the full package name of the target.
Then you should declare the Color enum like this.
public enum Color {
RED("package.Red"),
BLUE("package.Blue"),
GREEN("package.Green");
private final String value;
private Color(String value) {
this.value = value;
}
public String getValue() {
return value;
}
}
You may need a specific instance, not just a Class instance.
try {
Class clazz = Class.forName(Color.RED.getValue());
if (clazz.isInstance(Red.class)) {
Red red = (Red)clazz.cast(Red.class);
} else if (clazz.isInstance(Blue.class)) {
Blue blue = (Blue)clazz.cast(Blue.class);
} else if (clazz.isInstance(Green.class)) {
Green green = (Green)clazz.cast(Green.class);
}
} catch (ClassNotFoundException classNotFound) {
// A class named "package.Red" cannot be found
}