valueOf() is a static method of java.lang.Enum which takes argument as String.

valueOf() method returns the enum constant for the specified string value which is passed as an argument in the method if it exists as enum constant.

e.g.

package com.masterjavatutorial;
public enum MONTHS {
    JANUARY,FEBRUARY,MARCH,APRIL,
    MAY,JUNE,JULY,AUGUST,
    SEPTEMBER,OCTOBER,NOVEMBER,DECEMBER;
}

MONTHS month = MONTHS.valueOf(“JANUARY”);

This will return enum constant for “JANUARY”.

If the constant does not exists, then it would throw java.lang.IllegalArgumentException.

Demo Example 1 When constant exists : Enum valueOf() method

MONTHS.java

package com.masterjavatutorial;
public enum MONTHS {
    JANUARY,FEBRUARY,MARCH,APRIL,
    MAY,JUNE,JULY,AUGUST,
    SEPTEMBER,OCTOBER,NOVEMBER,DECEMBER;
}

Test.java

package com.masterjavatutorial;

public class Test {

	public static void main(String args[]) {

		MONTHS valueOf1 = MONTHS.valueOf("APRIL"); 
		System.out.println(valueOf1); 

		MONTHS valueOf2 = MONTHS.valueOf("FEBRUARY"); 
		System.out.println(valueOf2); 
        
	}
	
}

Output of the above Program is as below:

APRIL
FEBRUARY

Demo Example 2 When constant does not exists:Enum valueOf() method

package com.masterjavatutorial;
public enum MONTHS {
    JANUARY,FEBRUARY,MARCH,APRIL,
    MAY,JUNE,JULY,AUGUST,
    SEPTEMBER,OCTOBER,NOVEMBER,DECEMBER;
}

Test.java

package com.masterjavatutorial;

public class Test {

	public static void main(String args[]) {

		MONTHS valueOf1 = MONTHS.valueOf("APRIL123"); 
		System.out.println(valueOf1); 
	}
	
}

The above program would throw an exception as the constant “APRIL123” does not exists.
Output of the above Program is as below:

Exception in thread "main" java.lang.IllegalArgumentException: No enum constant com.masterjavatutorial.MONTHS.APRIL123
	at java.lang.Enum.valueOf(Enum.java:238)
	at com.masterjavatutorial.MONTHS.valueOf(MONTHS.java:1)
	at com.masterjavatutorial.Test.main(Test.java:7)