Java Program to Convert Enum to String
Given an enum containing a group of constants, the task is to convert the enum to a String.

Methods:
We can solve this problem using two methods:
- Using name() Method
- Using toString() Method
Let us discuss both of them in detail and implementing them to get a better understanding of the same.
Method 1: Using name() Method
It returns the name of the enum constant same as declared in its enum declaration.
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.
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.