How to Convert Enum to String

JavaJavaBeginner
Practice Now

Introduction

The enum data type in Java is useful for defining constants such as the days of the week or months of the year. However, sometimes we need to convert these constants to strings. Knowing how to do this is essential for working with APIs and databases.

Create the Enum

First, create an enum with constants that represent the values you want to convert to strings. In this example, we will use the Course enum.

enum Course {
  JAVA, ANDROID, HTML, CSS
}

Convert Enum to String using name() Method

The name() method of an enum returns the name of the constant as a string. To convert an enum to a string using this method, we loop through the values of the enum and call name() on each constant.

Course[] courses = Course.values(); // get all enum values
for (Course course : courses) {
  System.out.println(course.name()); // print name of enum constant as string
}

Output:

JAVA
ANDROID
HTML
CSS

Convert Enum to String using toString() Method

We can also convert an enum to a string by overriding the toString() method of the enum. This method should return the string representation of the enum value. In this example, we will add a course_name field to the Course enum and override toString() to return this field.

enum Course {
  JAVA("Java"), ANDROID("Android"), HTML("HTML"), CSS("CSS");

  String course_name;

  Course(String course_name) {
    this.course_name = course_name;
  }

  @Override
  public String toString() {
    return course_name;
  }
}

To convert the enum to a string using this method, we loop through the values of the enum and call toString() on each constant.

Course[] courses = Course.values(); // get all enum values
for (Course course : courses) {
  System.out.println(course.toString()); // print enum as string using toString() method
}

Output:

Java
Android
HTML
CSS

Alternative Ways to Declare Constants

There are other ways to declare constants in Java besides enums. One method is to use static final fields in a class.

class Course {
  public static final String JAVA= "Java";
  public static final String ANDROID= "Android";
  public static final String HTML= "HTML";
  public static final String CSS= "CSS";
}

We can access these constants directly by using the class name and constant name:

System.out.println(Course.JAVA); // print constant string

Output:

Java

Another method is to use an interface to declare constants:

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

We can access these constants directly by using the interface name and constant name:

System.out.println(Course.JAVA); // print constant string

Output:

Java

Summary

Converting enums to strings is a common task in Java programming. We can use the name() method or the toString() method of an enum to convert it to a string. We can also declare constants using static final fields or an interface instead of an enum.

Other Java Tutorials you may like