Having something like:
import com.example.test.classA;
import com.example.test.classB;
import com.example.test.classC;
public class MyClass {
...
Flavors p = Flavors.VANILLA;
...
String flavorChoice = "Flavors.CHOCOLATE";
...
}
where Flavors
is an enum
declared in one of the imported classes:
public enum Flavors { CHOCOLATE, VANILLA, LEMON };
How can I get a value, using reflection, from the string flavorChoice
?
Do not assume I know in which imported class Flavors
is declared.
Do not even assume that Flavors
is accessible from within MyClass
.
CLARIFICATIONS
The string flavorChoices
should produce an Object
, the object may be, in the example above it is, of type Flavors
. But it also may refer to other constants in other enums:
String flavorChoice = "ExtraFlavors.MENTA";
String flavorChoice = "ExoticFlavors.CHALK";
String flavorChoice = "AnyEmun.ANYCONSTANT";
I don't know what the string contents are until runtime. I want to be able to:
- Check the referred enum indeeed exists and is accessible from MyClass
- Get the referred enum constant and put it in an
Object