Java Enum valueOf() MethodThe valueOf() method of Enum class returns the enum constant(of defined enum type) along with the defined name. SyntaxType Parameters:T: It is the enum type whose constant is yielded. ParametersenumType - It is the Class object of enum type which returns a constant name - It is the name of constant to be returned Return ValueThe valueOf() method returns the enum constant along with the defined name. ThrowsThe valueOf() method throws:
Example 1Test it NowOutput: The part which is exposed to the environment is : 1 Skin 2 Muscles 3 Bones 4 Organs 5 Tissue Ans: Skin Example 2Test it NowOutput: Exception in thread "main" java.lang.IllegalArgumentException: No enum constant com.javaTpoint.Flower. The part which is exposed to the environment is : atjava.lang.Enum.valueOf(Enum.java:238) atcom.javaTpoint.Flower.valueOf(Enum_valueOfMethodExample2.java:4) at com.javaTpoint.Enum_valueOfMethodExample2.main(Enum_valueOfMethodExample2.java:11)
Next TopicJava Enum
|