HOWTO · Java

How to Convert Enum to String in Java

Learn how Java enum constants become strings with name(), toString(), custom display text, and values().

On this page

An enum defines a fixed set of named constants. Each constant is an instance of its enum type, and Java provides methods for obtaining a string representation. The method you choose depends on whether you need the exact identifier declared in source or text intended for display.

Convert an enum constant with name()

Call name() on a constant when you need its exact declared identifier. For example, Directions.WEST.name() returns the string WEST. The returned value preserves the spelling and capitalization used in the enum declaration. name() is final, so an enum cannot change that result by overriding it.

This is useful when code needs the identifier itself, such as when comparing a value with a known constant name. Keep in mind that renaming the enum constant changes the string returned by name(). If the text is meant for people to read, an identifier such as NORTH_EAST may not be the wording you want to show.

Convert an enum constant with toString()

Calling toString() also gives you a string. For an enum that does not override this method, the default result is the constant name, so Directions.WEST.toString() returns WEST. Unlike name(), toString() can be overridden by the enum to return different text.

Choose based on the meaning of the value. Use name() when the declared identifier matters; use toString() when the enum’s chosen string representation is appropriate for the caller. Neither choice is automatically best for every use. If another program or stored format depends on a stable identifier, define and manage that format deliberately instead of assuming display text will never change.

Override toString() for custom text

An enum can associate a string with each constant through a constructor and return that field from an overridden toString(). This lets the code keep concise identifiers such as USD while producing friendlier text such as US dollars. Each constant supplies its own value when the enum is declared.

In the example below, name() continues to return the declared identifier, while toString() returns the custom label. This distinction is useful in logs or user-facing output when the custom wording is suitable. Because callers of toString() receive the customized value, code that specifically needs USD should call name() instead.

Java provides a generated values() method for each enum type. It returns an array containing that type’s constants in declaration order; it is not a method inherited from Enum. Loop over that array to process every value. Concatenating an enum constant with text calls its toString() method, so the loop below prints the custom labels.

Run this complete example to compare exact names, default strings, customized display text, and every currency constant:

public class EnumToString {
    enum Directions {
        NORTH, SOUTH, EAST, WEST
    }

    enum PlainCurrencies {
        USD, YEN, EUR, INR
    }

    enum Currencies {
        USD("US dollars"),
        YEN("Japanese yen"),
        EUR("euros"),
        INR("Indian rupees");

        private final String displayText;

        Currencies(String displayText) {
            this.displayText = displayText;
        }

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

    public static void main(String[] args) {
        System.out.println(Directions.WEST.name());
        System.out.println(PlainCurrencies.USD.toString());
        System.out.println(Currencies.INR.name());
        System.out.println(Currencies.INR.toString());

        for (Currencies currency : Currencies.values()) {
            System.out.println(currency);
        }
    }
}
WEST
USD
INR
Indian rupees
US dollars
Japanese yen
euros
Indian rupees