valueOf Example in Java Enum
valueOf method of Java Enum is used to
retrieve Enum constant declared in Enum Type by passing String in other words valueOf method is
used to convert
String to Enum constants. In this Java Enum valueOf example we
will see how to use valueOf method in Java. valueOf method is
implicitly available to all Java
Enum because every enum in Java implicitly extends java.lang.Enum class. valueOf method of
enum accepts exactly same String which is used to declare Enum constant to
return that Enum constant. valueOf method is case-sensitive
and invalid String will result in IllegalArgumentException. In short
String passed to valueOf method of Java enum must be same
to String returned by name() method like TrafficSigal.RED.name() returns
RED and you should pass RED to valueOf() to get TrafficSignal.RED.