Enum to String in Java
An “enum” is a vital feature in Java that enables the developer to store the data with flexible limitations. For instance, keeping the data inside or outside the class in accordance with the requirement. In such cases, utilizing an enum and transforming it into a string in Java is assistive in storing and effectively accessing the data as compared to other approaches.
This article will discuss the methodologies to convert an enum to a string in Java.
What is an “enum” in Java?
The “enum” is an abbreviated form of enumeration. This keyword is used to create an enum, and it is a special class that corresponds to the constants group. These constants are declared inside the “enum” and are separated by a comma. You can also declare both inside and outside the class.
How to Convert/Transform an Enum to String Using Java?
An enum can be transformed into a string using Java using the following approaches:
Approach 1: Convert an Enum to String in Java Using the “toString()” Method
The “toString()” method is utilized to fetch a string object which corresponds to the value of the number object and can be overwritten. This method can be utilized to simply transform the enum values into strings by referring to the enum.
Syntax
The above syntax returns the enum constant’s name.
Example
Go through the below-provided example:
In the above lines of code:
- Create an enum named “data” accumulating the stated values in capitals.
- Note that the enum here is declared within the class.
- Finally, use the “dot” syntax to point to the enum values directly and display them in string representation via the “toString()” method.
Output
In the above output, it can be observed that the values are accessed and displayed as a string.
Approach 2: Convert/Transform an Enum to String in Java Using the “name()” Method
The “name()” method of the Enum class gives the enum constant’s name the same as declared in its(enum) declaration. This method, unlike the “toString()” method, is final and cannot be overwritten.
Syntax
In the above syntax, the “name()” method gives the name of this enum.
Example
Let’s overview the below-provided example:
In the above code snippet:
- Likewise, define an enum named “data” containing the given values.
- In this particular approach, the “enum” is declared outside the class.
- Now, refer to the contained values similarly and fetch the string representation using the associated “name()” method.
Output

As you can see that the string representation of the enum has been fetched appropriately.
Conclusion
An enum named as an enumeration in Java can be converted into a string using the “toString()” method or the “name()” method. Both of these approaches transform the enum specified inside and outside the class into strings, respectively. This blog stated the approaches to convert enum to a string in Java.
About the author

Umar Hassan
I am a Front-End Web Developer. Being a technical author, I try to learn new things and adapt with them every day. I am passionate to write about evolving software tools and technologies and make it understandable for the end-user.
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
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.
Tech Tutorials
Tutorials and posts about Java, Spring, Hadoop and many more. Java code examples and interview questions. Spring code examples.
Saturday, July 4, 2020
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 with Example
Since Enum in Java allows a programmer to override an inherited method and since Enum has access to all Object class methods, you can easily override the toString() method to provide a custom String implementation for an Enum instance which can further use to convert that Enum instance to String in Java.
Both ways of converting Enum to String have their own merit and use cases. Prefer name() method if String representation of Enum instance is its declared name and prefer toString() method if every Enum Instance has its own custom String representation.
For example, if the declared enum is RED and you want text representation as «RED» then use the name() method because both are the same, but if you want String representation as «Red» then you can use the toString() method to customize that bit.
It’s actually better to use toString() from a maintenance perspective because if any developer refactors and changes the name of Enum constant then its String representation will also change, which is not a desirable coupling and should be avoided at all costs.
Java Example to Convert Enum to String

/**
* Java program to demonstrate how to convert Enum to String in Java. This program demonstrates
* two examples by using the name() and toString() method to get a meaningful String representation
* from an Enum instance in Java.
*
* @author Javin Paul
*/
public class EnumToString <
private enum Weekdays <
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY ;
>
private enum ColdDrink <
PEPSI ( «Pepsi» ) , COKE ( «Coca Cola» ) , SPRITE ( «Sprite» ) ;
private String brandname ;
private ColdDrink ( String brand ) <
this . brandname = brand ;
>
@Override
public String toString () <
return brandname ;
>
>
public static void main ( String args []) <
//Converting Enum to String by using name() method
//by default print mehtod calls toString() of enum
ColdDrink [] drinks = ColdDrink. values () ;
for ( ColdDrink drink : drinks ) <
System. out . printf ( «String to Enum example using name : %s%n» , drink. name ()) ;
>
//Converting Enum to String using toString() method
for ( ColdDrink drink : drinks ) <
System. out . println ( «String to enum conversion using toString() : » + drink ) ;
>
>
>
Output:
String to Enum example using name: PEPSI
String to Enum example using name: COKE
String to Enum example using name: SPRITE
String to enum conversion using toString () : Pepsi
String to enum conversion using toString () : Coca Cola
String to enum conversion using toString () : Sprite
That’s all on How to convert Enum into String in Java. Enum provides different ways to return meaningful String representation with utmost flexibility offered by allowing overriding toString method in Java. For most cases name() method would be enough to convert Enum to String.