Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

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.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW