Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Overview

An enum is a special type of class introduced in Java 5 to help replace the int enumeration pattern. While technically suitable, the names we use for our enum constants are often not the names we’d like to show up in our logs, databases, or customer-facing portions of our application.
In this tutorial, we’ll learn about the various ways of implementing toString() on enums in Java so that we can supply alternate or decorative names.

2. Default Behavior

All enums implicitly extend the Enum class, so that’s where our enum will inherit its default toString() behavior from:

public String toString() {
    return name;
}

3. Override toString() on enum Type

Since we don’t have access to the Enum class, the next highest place where we can control the toString() behavior is in the enum type declaration:

enum FastFood {
    PIZZA,
    BURGER,
    TACO,
    CHICKEN,
    ;

    @Override
    public String toString() {
        // ...?
    }
}

The only variables we have access to inside the toString() method are this.name and this.ordinal. We already know that the default behavior is to print the enum constant name, and we’re not interested in printing the ordinal. We could, however, map the ordinals one-to-one to our decorated strings:

@Override
public String toString() {
    switch (this.ordinal()) {
        case 0:
            return "Pizza Pie";
        case 1:
            return "Cheese Burger";
        case 2:
            return "Crunchy Taco";
        case 3:
            return "Fried Chicken";
        default:
            return null;
    }
}

While effective, it’s not very expressive to say 0 = Pizza Pie. However, because this is itself an enum, we can simplify the above switch statement to be more straightforward:

@Override
public String toString() {
    switch (this) {
        case PIZZA:
            return "Pizza Pie";
        case BURGER:
            return "Cheese Burger";
        case TACO:
            return "Crunchy Taco";
        case CHICKEN:
            return "Fried Chicken";
        default:
            return null;
    }
}

4. Override toString() on enum Constants

We can override any methods defined in the Enum class or enum type declaration on each of the individual enum constants. Since we know toString() is defined in the Enum class, we can skip implementing it in our enum type declaration:

enum FastFood {
    PIZZA {
        @Override
        public String toString() {
            return "Pizza Pie";
        }
    },
    BURGER {
        @Override
        public String toString() {
            return "Cheese Burger";
        }
    },
    TACO {
        @Override
        public String toString() {
            return "Crunchy Taco";
        }
    },
    CHICKEN {
        @Override
        public String toString() {
            return "Fried Chicken";
        }
    }
}

5. Alternate Name Field

Another option we have is to include additional fields in our enum type declaration and assign values that will be passed to the constructor when each enum constant is created:

enum FastFood {
    PIZZA("Pizza Pie"),
    BURGER("Cheese Burger"),
    TACO("Crunchy Taco"),
    CHICKEN("Fried Chicken"),
    ;

    private final String prettyName;

    FastFood(String prettyName) {
        this.prettyName = prettyName;
    }
}

Then, we can simply utilize those values in the toString() method of our enum type declaration:

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

6. Create enum from a Decorated String

If we’re going to do all this work to convert our enums into decorated Strings, it makes sense to build in a way to do the same thing in reverse. Rather than override the valueOf() method of the Enum class, which compares the supplied String to the enum constant name, let’s create our own so that we can utilize either behavior if we wish to:

FastFood fromString(String prettyName) {
    for (FastFood f : values()) {
        if (f.prettyName.equals(prettyName)) {
            return f;
        }
    }
    return null;
}

7. Conclusion

In this article, we’ve learned several ways to implement toString() for enums. As always, the full source code used in the article can be found over on GitHub.

Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.