Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

Enum.name(): the exact constant name

Enum.name() returns an enum constant's identifier exactly as declared. Unlike toString(), it cannot be overridden.

Version: Java SE 21; repository runtime, production runtime not attested

Syntax

public final String name()
// Usage: enumConstant.name()

Enum.name is an instance method with the signature public final String name(). It returns the constant's name exactly as declared, preserving case.

name() is final, so it cannot be overridden. The default toString() returns the same name, but can be overridden to provide a more readable description.

Use name() when you need the exact identifier rather than a display label. Renaming the constant changes the result; when persisting these names, treat such a rename as a data-format change.

Examples

public class Main {
    enum Status {
        READY;

        @Override
        public String toString() {
            return "Ready to start";
        }
    }

    public static void main(String[] args) {
        System.out.println(Status.READY.name());
        System.out.println(Status.READY.toString());
    }
}

Result

READY
Ready to start

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources