Javatpoint Logo
Javatpoint Logo

How to Convert a String to Enum in Java?

Java enum are powerful data types that represent a fixed set of constants. They are commonly used to outline a specific set of values that an object can take. Sometimes, you may want to convert a string illustration into an enum value. In this context, we will explore diverse strategies to convert a string to an enum in Java, offering you with a whole program for a better understanding.

Understanding Enum in Java:

Before diving into the conversion process, let's briefly recap Java enum. Enumerations, or enum for short, were introduced in Java 5 to provide a more structured way of representing constant values. An enum declaration defines a fixed set of values that a variable of that enum type can take. Each value in an enum is called an enum constant.

Here's an example of an enum declaration:

In this case, Day is the name of the enum, and it has seven constants representing the days of the week. Enum can also have additional fields and methods, making them more flexible and powerful than simple constant values.

Converting a String to Enum:

When you've got a string representation and also you want to convert it to an enum, you could follow different tactics based to your requirements. Here, we will speak 3 common methods for acting this conversion: the usage of the valueOf() method, the use of a custom mapping, and the use of the Enum.ValueOf() method.

Method 1: Using valueOf() Method

The maximum direct manner to convert a string to an enum is with the aid of the usage of the valueOf() technique furnished by means of the enum itself. This approach takes the name of the enum consistent as a string and returns the corresponding enum regular if discovered. Here's an example:



In this example, we convert the string "FRIDAY" to the Day enum by calling the valueOf() method. The result is the Day.FRIDAY enum constant, which is then printed to the console.

It's important to note that the valueOf() method is case-sensitive, and if the string does not match any enum constant, a java.lang.IllegalArgumentException will be thrown. To handle this, you can wrap the conversion in a try-catch block to handle the exception gracefully.

Method 2: Using a Custom Mapping

In some cases, you may need to perform a custom mapping between the string representation and the enum constant. This approach is useful when the string values don't match the enum constant names exactly. Here's an example:



In this example, the Day enum has an additional field called abbreviation, which represents a shorter string representation of the day. We define a custom method fromAbbreviation() that performs the conversion based on the abbreviation. If the abbreviation matches any enum constant, the corresponding enum constant is returned; otherwise, an exception is thrown.

Method 3: Using the Enum.valueOf() Method

The third method involves using the Enum.valueOf() method, which is a general-purpose method for converting strings to enum constants. Here's an example:



In this example, we pass the enum class (Day.class) and the string representation (dayString) to the Enum.valueOf() method. It returns the corresponding enum constant if found, or throws an IllegalArgumentException if no match is found.

In this article, we explored different methods for converting a string to an enum in Java. We discussed using the valueOf() method, creating a custom mapping, and utilizing the Enum.valueOf() method. Depending on your requirements, you can choose the most appropriate approach. Remember to handle exceptions when using the valueOf() method, and consider a custom mapping when the string values don't match the enum constant names directly.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA