The other responses about using valueOf(string) are right on, but I would add that you should protect your code for the potential for an invalid string to be passed. While today your code may only pass strings that correspond with valid enum values it is possible for a future change to the enum or a future change to other parts of your code (or code written by others) to break this assumption.
This will be important for cases like when writing a setter for a class's enum member variable. Your class and its setter should protect themselves.
The JavaDocs for Enum show that an exception could be thrown on this (IllegalArgumentException or NullPointerException. You should handle these either generically or specifically. The JavaDocs also show that this function won't like "extraneous whitespace" ... so consider using trim().
I would probably handle the exceptions generically like this:
Enum BestEnumEver {OPTION_A, OPTION_B, OPTION_C}
// ... elsewhere in your code ...
// How confident are you that stringVar will have an acceptable value?
// How confident are you that the existing enum options will not need to change?
BestEnumEver enumValue;
try {
// Consider using trim to eliminate extraneous whitespace
enumValue = BestEnumEver.valueOf(stringVar.trim());
} catch (Exception e) {
// handle the situation here. Here are a couple of ideas.
// Apply null and expect the using code to detect.
enumValue = null;
// Have a defined private constant for a default value
// assuming a default value would make more sense than null
enumValue = DEFAULT_BEST_ENUM_EVER;
}
But you could also handle each exceptions in some unique way.
Enum BestEnumEver {OPTION_A, OPTION_B, OPTION_C}
// ... elsewhere in your code ...
// How confident are you that stringVar will have an acceptable value?
// How confident are you that the existing enum options will not need to change?
BestEnumEver enumValue;
try {
// Consider using trim to eliminate extraneous whitespace
enumValue = BestEnumEver.valueOf(stringVar.trim());
} catch (IllegalArgumentException e1) {
// handle the situation here ...
} catch (NullPointerException e2) {
// handle the situation here ...
}