Enum to String in Java
Enum in Java is a special data type or class that holds a set of constants. We can add constructors and methods in an enum, too. To create an enum in Java, we use the keyword enum and give it a name just like a class. In this article, we will go through the ways to convert an enum to string Java.
Convert Enum to String Using name() in Java
Please enable JavaScript
In the first example, we will use name() of the Enum class to return the enum constant’s exact name as a string. Below, we have created an enum inside the class, but we can create an enum either outside or inside it. We named the enum Directions , which contains the names of directions as enum constant.
We can fetch any constant using the name() method. Directions.WEST.name() will return WEST as a string, which is stored in the string variable getWestInString and then print in the output.
Convert Enum to String Using toString() in Java
Just like name() we have the toString() method, but when it comes to using an enum constant for any important purpose, name() is preferred because it returns the same constant while toString() can be overridden inside the enum. It means that we can modify what is returned as a string using toString() , which we will see in the next example.
In this example, we use the toString() method on the constant that needs to be converted to a string.
We have discussed above that we can override the toString() method to modify what we want to return as a string with the enum constant. In the example below, we have four currencies as constants calling the enum constructors with a string passed as an argument.
Whenever a constant sees a toString() method, it will pass the string name to getCurrencyName , a string variable. Now we have to override the toString() method inside the enum and return the getCurrencyName with a string.
Converting Enum to String in Java
Java enums are powerful constructs that allow developers to define a set of named constants. However, there are scenarios where we may need to convert an enum value to a string representation. In this blog, we will explore different methods to convert Java enums to strings and discuss their pros and cons. We will provide code examples for each method, along with explanations and output.
Method 1: Using the name() Method
The name() method is available for all Java enums by default. It returns the name of the enum constant as a string, which can be used for converting an enum to a string representation. However, it does not provide much flexibility for customizing the string representation and may not be suitable for all use cases.
Output:
Method 2: Implementing a toString() Method
Another approach is to override the toString() method in the enum class. This allows us to provide a custom string representation for each enum constant. By implementing toString() , we can tailor the output according to our requirements.
Output:
Method 3: Using a Map for Custom String Conversion
In cases where we need more complex mappings from enums to strings, we can utilize a Map to store the desired string representations. This method provides the flexibility to define custom conversions and allows for easy modifications in the mapping.
Output:
Method 4: Using the valueOf() Method
The valueOf() method allows us to convert a string back to an enum constant. By utilizing this method in reverse, we can perform the enum to string conversion.
Output:
Conclusion:
In this blog, we explored different methods for converting Java enums to strings. We discussed the usage of the name() method, implementing a custom toString() method, utilizing a Map for custom string conversion, and leveraging the valueOf() method in reverse. The choice of method depends on the specific requirements of the application. By understanding these techniques, developers can effectively convert enums to strings and enhance the flexibility and readability of their code.
Tech Tutorials
Tutorials and posts about Java, Spring, Hadoop and many more. Java code examples and interview questions. Spring code examples.
Sunday, March 12, 2023
Converting Enum to String in Java
In this post we’ll see the options we have to convert an Enum to String in Java. It may be needed when you want to compare Enum to String in Java.
Converting Enum to String in Java
- name()— Returns the name of this enum constant, exactly as declared in its enum declaration.
- toString()— Returns the name of this enum constant, as contained in the declaration.
As per Java docs toString() should be preferred. That’s what the description of the name() method says “Most programmers should use the toString() method in preference to this one, as the toString method may return a more user-friendly name”. This is because of the fact that toString() can be overridden if need be to return a more «programmer-friendly» string form.
Converting Enum to String using name() method
Following example shows how to convert Enum to String in Java using name() method. In the example Enum constants are iterated using the values() method, in each iteration Enum type is converted to String using the name() method.
Converting Enum to String using toString() method
Following example shows how to convert Enum to String using toString() method. In the example toString() is overridden with in the Enum type to return a short form of the day. Note that it is not always required to override toString() method, here it is done just to demonstrate how it can be used to return a more «programmer-friendly» string form.
That’s all for this topic Converting Enum to String in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!
How to Convert Enum to String in Java
In this article, we are going to convert the enum to String in Java. enum is very handy in terms of defining constants in Java. The enum is a special type of data type that is a set of predefined constants. Java provides it to collect constants in a single place.
There are 2 ways by which we can convert the enum to String in Java.
- using an inbuilt name() method
- using an inbuilt toString() method
Convert enum to String using an inbuilt name() method
In the below code, all the values of enum constants are stored in the array of an enum, then at the time of traversing this array we called the name() method which will return the corresponding constant in String format.
Name of All Courses
JAVA
ANDROID
HTML
CSS
This is the easiest way to convert enum to String in java but, according to java documentation:
The name() method is designed primarily for use in specialized situations where correctness depends on getting the exact name, which will not vary from release to release.
Convert enum to String using an inbuilt toString() method
In this second method, we are going to take advantage of the Java feature which allows the programmer to override an inherited method. By simply overriding the toString() method, we can add the functionality of getting String.
Name of All Courses
JAVA
ANDROID
HTML
CSS
Here, you should keep in mind the toString() method must be public otherwise it will give an error like this:
error: toString() in Course cannot override toString() in Enum String toString() attempting to assign weaker access privileges;
The above two ways are the correct way of converting enum to String in Java but other than these two methods there are other ways too by which we can do the same thing.
Static final Strings in a Class instead of enums
The static final strings variables are accessible outside of the class same as enum and value does not change, so this is also another way to use string constants directly instead of an enum.
Example
It is an alternate to enum, we can declare the public static final String variables in a class and it will be available for direct access outside of the class as constants. See the example below.
Interface to declare constants
We can use interface to declare constants. By using the interface we do not need to convert anything, since all the strings will publicly visible same as constants in an enum.
Example
Here, we declared one interface and inside of the interface strings are declared as a constant. We can access them directly by using interface name and constant name.
Conclusion:
There are mainly two methods toString() and name() by which we can convert the enum to String but we are not restricted with these two methods only and there are other alternatives like interfaces that also exists.