Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

EnumClass.values() — all enum constants

EnumClass.values() returns an array of all constants of an enum in declaration order. Use it to visit them in a for loop.

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

Syntax

EnumClass.values() // public static EnumClass[] values()

EnumClass stands for a specific enum type name. The compiler implicitly declares its public static EnumClass[] values() method. Calling EnumClass.values() takes no arguments and returns an array of constants in declaration order, not alphabetical order.

This method belongs to the specific enum; it is not inherited from java.lang.Enum. Use, for example, Status.values(), not Enum.values(). The result works with an enhanced for loop; the example prints every constant's name.

Examples

public class Main {
    enum Status { NEW, ACTIVE, DONE }

    public static void main(String[] args) {
        for (Status status : Status.values()) {
            System.out.println(status.name());
        }
    }
}

Result

NEW
ACTIVE
DONE

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