Signup/Sign In

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.

enum Course
{
	JAVA, ANDROID, HTML, CSS
}

public class StudyTonight {

	public static void main(String[] args) {
		// here values() method returns all the instances of Enum
		Course[] courses = Course.values();
		System.out.println("Name of All Courses");
		for(Course course: courses)
		{
			System.out.println(course.name());
	    }
	}
}


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.

public class StudyTonight {
	enum Course {

		JAVA("Java"), ANDROID("Android"), HTML("HTML"), CSS("CSS");
		String course_name;
		//Constructor to define name
		Course(String course_name) {
			this.course_name = course_name;
		}
		//override the inherited method
		@Override
		public String toString() {
			return course_name;
		}
	}
	public static void main(String args[]) {
		Course[] courses = Course.values();
		System.out.println("Name of All Courses");
		for (Course course: courses) {
			System.out.println(course.toString());
		}
	}
}


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.

class ClassName{
	public static final String CONSTANT= "CONSTANT";
}

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.

class Course
{
    //declaration of all constant terms same as constants in enum
    public static final String JAVA= "Java";
    public static final String ANDROID= "Android";
    public static final String HTML= "HTML";
    public static final String CSS= "CSS";
}

public class StudyTonight
{
    public static void main(String args[])
    {
            //this print statements will print all the constants
            System.out.println(Course.JAVA);
            System.out.println(Course.ANDROID);
            System.out.println(Course.HTML);
            System.out.println(Course.CSS);
    }
}


JAVA
ANDROID
HTML
CSS

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.

public interface InterfaceName{
    String CONSTANT = "CONSTANT";
}

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.

interface Course
{
    String JAVA= "Java";
    String ANDROID= "Android";
    String HTML= "HTML";
    String CSS= "CSS";
}

public class StudyTonight
{
    public static void main(String args[])
    {
            System.out.println(Course.JAVA);
            System.out.println(Course.ANDROID);
            System.out.println(Course.HTML);
            System.out.println(Course.CSS);
    }
}


JAVA
ANDROID
HTML
CSS

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.



About the author:
I am a 3rd-year Computer Science Engineering student at Vellore Institute of Technology. I like to play around with new technologies and love to code.