I have an enum as below:
public enum EnvironmentType {PRODUCTION, TEST, DEVELOPMENT}
in properties file the key value is like :
app.environmentType = TEST
we know that when i read the value from properties file using key, it returns as String like
String envType = properties.getProperty("app.environmentType");
My requirement is,
EnvironmentType envType = EnvironmentType.TEST;
Now i want to know that is there a way to get the value as enum
type? how can i parse
or cast
it ?
EnvironmentType envType = EnvironmentType.valueOf(envTypeString);
You may use valueOf
method as follows:
String envTypeStr = properties.getProperty("app.environmentType");
EnvironmentType envType = EnvironmentType.valueOf(envTypeStr);
The static methods valueOf()
and values()
are created at compile time and do not appear in source code. But they appear in documentation of some enumaration in java library. For example, see SortOrder
and Normalizer.Form
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With