An Enum is a data type in Java that includes a predetermined set of constants. Days of the week, Colors are some of the common examples of Enum. Similar to classes, you can use Enums to specify your own data types. An Enum can be stated both outside and inside a Class, but not within a Method.
In Java, the “enum” keyword can be utilized to define an Enum type. An enum cannot inherit any other class because it inherits from the Java Enum class internally; however, it can use a variety of interfaces.
This article will explain the use of the valueOf() method of the Enum class in Java.
How to Use valueOf() Method of Enum Java Class?
The “valueOf()” method of the Enum class is utilized to get an enum constant of the stated enum type with its name. The exact String that is used to declare the Enum constant is passed to the valueOf() method, which returns the Enum constant. It is a case-sensitive method. This method also throws an exception if you try to access an invalid string.
Syntax
The following syntax is utilized for the “valueOf()” method:
Here, “enum” is the name of the declared enum used to invoke the “valueOf()” method by passing String as a “constantValue”.
Example 1: Accessing an Enum Constant Value
Firstly, we will create an enum named “ProgrammingLanguages” using the “enum” keyword that contains the constant values:
C, Java, Python
}
In the main() method of the “Example” class, we will first create an object “plang” of the enum ProgrammingLanguages that store the constant value and then invoke the “valueOf()” method with enum by passing the String “Java” that will be used to get the constant from the enum:
System.out.println("enum constant: " + plang);
The output displayed the value of the specified enum constant:
Let’s see what happens when we call the element that does not exist in the enum type.
Example 2: Accessing a Non-existing Enum Constant Value
We will consider the previously created enum in this example and get the value of the constant “C++” that is not in the ProgrammingLanguages enum:
System.out.println("enum constant: " + plang);
As a result, the compiler will throw an exception:
Let’s see what happens when we call the null in the enum.
Example 3: Accessing a null Enum Constant Value
We will pass the null String in the “valueOf()” method. It will also throw an exception because we have not added any null constant in the created enum:
System.out.println("enum constant: " + plang);
Output
Let’s see how to print all the constants of the enum.
Example 4: Accessing All Enum Constant Values at Once
In the main() method of the class “Example”, we will print all the constant values of the enum using the “values()” method in the “for” loop. To do so, we will use the “ordinal()” method to get the enum constant with an index, like an array index. Lastly, the valueOf() method will print all the constants of the enum by using “System.out.println()” method:
for(ProgrammingLanguages pl : ProgrammingLanguages.values()) {
int i = pl.ordinal()+1;
System.out.println(i+" "+pl);
}
The output displays all the constants of the enum named ProgrammingLanguages:
We covered all the basic instructions to use the valueOf() method of the Java Enum class.
Conclusion
The “valueOf()” method is utilized to get the constant of the enum. It accepts and returns the same String used during the declaration of the Enum constant. If the passed string is not the constant of the enum, it will throw an exception. Also, the method is case-sensitive. In this article, we explained the usage of valueOf() of the Enum class method with detailed examples.