Archive for October, 2013

October 21st, 2013
3:55 pm
Translating a string Enum value into an Enum given an Enum class object

Posted under Java

You can translate a string Enum into its corresponding Enum using the Enum in this way :-

 

//Assuming Fields is the target Enum
String fieldName = “myField”;
Fields field = Fields.valueOf(fieldName);

 

However this can also be done if you are given a class object of the Enum:-

 

//Assuming fieldClass is a class object for the Fields Enum
String fieldName = “myField”;
Fields field = Enum.valueOf(fieldClass, fieldName);

No Comments »