2 Ways to Print Custom String Value of Java Enum

We all know that how powerful enumeration type in Java is, and one of the main strength of enum is that they can implement an interface, they can have an instance variable and you can also override any method inside enum instance. In Java programs, we often need to convert Enum to String type, sometimes just to print values in log file and other time for storing log into database.  By default, when you print an enum constant, it print its literal value e.g. if name of enum instance is RED, then it will print RED. This is also the value which is returned by name() method of java.lang.Enum class. But, there are situations when we want a custom String value for enum constant. For example, I want to print Red instead of RED when I convert Enum to String. How do you do that? Well, there are two ways you can achieve this, first by overriding toString() method for each enum constant and second by using an instance variable to hold custom String value. You can provide custom value while creating Enum constants and later you can call that method which returns custom  String value. In this article, we will see example of these two ways.

Java Program to print custom String values to Enum in Java

As I said there are two ways you can associate a user-defined String value to an enum constant
  • By overriding toString() method
  • By Using an instance variable

In our example, we have an enumeration type called Color, it has three enum constants RED, GREEN,  and BLUE.  By default when you pass any of these enumeration constants to System.out.println() method, it will call toString() method of java.lang.Object. Since every enum by default extends java.lang.Enum, which overrides toString() method.

As shown in following code snippet, this overridden version returns the ame, which is the literal value of enumeration constant.

     * Returns the name of this enum constant, as contained in the
     * declaration.  This method may be overridden, though it typically
     * isn't necessary or desirable.  An enum type should override this
     * method when a more "programmer-friendly" string form exists.
     * @return the name of this enum constant
    public String toString() {
        return name;

You can see the nice advice given their in form of Javadoc comments. It clearly suggests that you should override toString() method in enum if you need more "programmer-friendly" string. Another way to provide a custom String is to store that value on a member value and return it by creating a custom method, remember you cannot create toString()  but can certainly create getCustomString(). Now you may think what is the difference between these two approach? Well, there is a significant difference. First way is actually the right way to provide custom String value because toString() is standard method for such task. Many framework and other method e.g. print() and println() method from System class, calls this method when you pass an enum. You don't get this awesome feature with any other method. Actually second way is extension of associating a custom value with enum, which could be integer, String or anything else. In our program, ColorWithToString overrides toString() to return custom String value and ColorWithSpecificString uses an instance variable to hold a specific String value.  here is full code example :
How to print custom String value of Java Enum

 * Couple of ways to provide custom String values to any enum instance in Java.
 * @author Javin Paual
public class EnumWithCustomString{

    public static void main(String args[]) {

        System.out.println("Default String value of Java Enum Color.RED is "
                + Color.RED);
        System.out.println("Custom String value of Java Enum  ColorWithToString.RED is "
                + ColorWithToString.RED);
        System.out.println("Custom String value of Enum in Java, ColorWithSpecificString.RED is "
                + ColorWithSpecificString.RED.getCustomString());

     * If you don't implement toString() method in Enum
     * constants, toString() will return their name e.g.
     * RED.toString() will return RED. This is also
     * same as calling RED.name() method.
    private enum Color {

        RED, GREEN, BLUE;

     * You can implement toString() method inside each enum
     * constant to provide a custom String value. For example
     * in below case RED.toString() will return "Red"
    private enum ColorWithToString {

        RED {

                    public String toString() {
                        return "Red";
        GREEN {

                    public String toString() {
                        return "Green";
        BLUE {

                    public String toString() {
                        return "Blue";

     * You can also provide custom String values for Java enum, by
     * storing them in separate field. In this case toString() will
     * remain unchanged, but you can get custom String value by calling
     * your method e.g. RED.toString() will return RED, but RED.getCustomString()
     * will return "Red".
    private enum ColorWithSpecificString {

        RED("red"), BLUE("blue"), GREEN("green");

        private String custom;

        private ColorWithSpecificString(String custom) {
            this.custom = custom;

        public String getCustomString() {
            return custom;



Default String value of Java Enum Color.RED is RED
Custom String value of Java Enum  ColorWithToString.RED is Red
Custom String value of Enum in Java, ColorWithSpecificString.RED is red

That's all about how to print custom string value of enum in Java. As suggested by Oracle Java developer's as well, the right way to return an String other than literal value is by overriding toString() method. This method is meant to provide a String value and used by many developers to convert an Enum to String as well. Second method also highlight, how you can use enum as full blown class, defining instance variable, creating methods and writing code there. You can use this technique to associate anything with your enum constant in Java.

Further Learning
Data Structures and Algorithms: Deep Dive Using Java
Java Fundamentals: The Java Language
Complete Java Masterclass

Other Java Enum Tutorials You may Like :
  1. Top 15 Java Enum Interview Questions with Answers (see here)
  2. Difference between RegularEnumSet and JumboEnumSet in Java (read here)
  3. What Every Java Programmer Should know about Enum (click here)
  4. 10 points about Enum in Java (see here)
  5. Learn how to use Enum in Java with a Simple Example (check here)
  6. Can Enum have Constructor in Java (learn here)
  7. Can we use Enum in Switch Statement in Java (Yes)
  8. How to use valueOf method of Enum (check here)
  9. How to loop over Enum constants in Java (example)
  10. Java tip to convert  Enum to String in Java (see tip)
  11. String to Enum in Java with Example (check here)

1 comment:

  1. I think you forget the most important message, use Enum instead of String. Anywhere you think you pass String which is fixed and well known, consider using enumeration type. Enum is better, robust and easy to use.


Feel free to comment, ask questions if you have any doubt.