Javatpoint Logo
Javatpoint Logo

Java Enum valueOf() Method

The valueOf() method of Enum class returns the enum constant(of defined enum type) along with the defined name.

Syntax

Type Parameters:

T: It is the enum type whose constant is yielded.

Parameters

enumType - It is the Class object of enum type which returns a constant

name - It is the name of constant to be returned

Return Value

The valueOf() method returns the enum constant along with the defined name.

Throws

The valueOf() method throws:

  1. IllegalArgumentException, if the defined enum type is inconstant with defined name or an enum type is not illustrated by the defined class object.
  2. NullPointerException, if enumType or name represents null value.

Example 1

Test it Now

Output:

The part which is exposed to the environment is :
1 Skin
2 Muscles
3 Bones
4 Organs
5 Tissue

Ans: Skin

Example 2

Test it Now

Output:

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



Help Others, Please Share

facebook twitter pinterest