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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL java(("`Java`")) -.-> java/ProgrammingTechniquesGroup(["`Programming Techniques`"]) java(("`Java`")) -.-> java/ObjectOrientedandAdvancedConceptsGroup(["`Object-Oriented and Advanced Concepts`"]) java(("`Java`")) -.-> java/BasicSyntaxGroup(["`Basic Syntax`"]) java(("`Java`")) -.-> java/DataStructuresGroup(["`Data Structures`"]) java(("`Java`")) -.-> java/StringManipulationGroup(["`String Manipulation`"]) java(("`Java`")) -.-> java/SystemandDataProcessingGroup(["`System and Data Processing`"]) java/ProgrammingTechniquesGroup -.-> java/method_overriding("`Method Overriding`") java/ProgrammingTechniquesGroup -.-> java/scope("`Scope`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/annotation("`Annotation`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/abstraction("`Abstraction`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/classes_objects("`Classes/Objects`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/class_methods("`Class Methods`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/enums("`Enums`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/interface("`Interface`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/modifiers("`Modifiers`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/oop("`OOP`") java/BasicSyntaxGroup -.-> java/identifier("`Identifier`") java/DataStructuresGroup -.-> java/arrays("`Arrays`") java/BasicSyntaxGroup -.-> java/comments("`Comments`") java/BasicSyntaxGroup -.-> java/data_types("`Data Types`") java/BasicSyntaxGroup -.-> java/operators("`Operators`") java/BasicSyntaxGroup -.-> java/output("`Output`") java/StringManipulationGroup -.-> java/strings("`Strings`") java/BasicSyntaxGroup -.-> java/variables("`Variables`") java/SystemandDataProcessingGroup -.-> java/object_methods("`Object Methods`") java/SystemandDataProcessingGroup -.-> java/string_methods("`String Methods`") java/SystemandDataProcessingGroup -.-> java/system_methods("`System Methods`") subgraph Lab Skills java/method_overriding -.-> lab-117421{{"`How to Convert Enum to String`"}} java/scope -.-> lab-117421{{"`How to Convert Enum to String`"}} java/annotation -.-> lab-117421{{"`How to Convert Enum to String`"}} java/abstraction -.-> lab-117421{{"`How to Convert Enum to String`"}} java/classes_objects -.-> lab-117421{{"`How to Convert Enum to String`"}} java/class_methods -.-> lab-117421{{"`How to Convert Enum to String`"}} java/enums -.-> lab-117421{{"`How to Convert Enum to String`"}} java/interface -.-> lab-117421{{"`How to Convert Enum to String`"}} java/modifiers -.-> lab-117421{{"`How to Convert Enum to String`"}} java/oop -.-> lab-117421{{"`How to Convert Enum to String`"}} java/identifier -.-> lab-117421{{"`How to Convert Enum to String`"}} java/arrays -.-> lab-117421{{"`How to Convert Enum to String`"}} java/comments -.-> lab-117421{{"`How to Convert Enum to String`"}} java/data_types -.-> lab-117421{{"`How to Convert Enum to String`"}} java/operators -.-> lab-117421{{"`How to Convert Enum to String`"}} java/output -.-> lab-117421{{"`How to Convert Enum to String`"}} java/strings -.-> lab-117421{{"`How to Convert Enum to String`"}} java/variables -.-> lab-117421{{"`How to Convert Enum to String`"}} java/object_methods -.-> lab-117421{{"`How to Convert Enum to String`"}} java/string_methods -.-> lab-117421{{"`How to Convert Enum to String`"}} java/system_methods -.-> lab-117421{{"`How to Convert Enum to String`"}} end

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