Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

Java Dictionary

  1. Home
  2. Java Dictionary
  3. enum

enum

An enum (enumeration type) lets you define a fixed set of named constants as a type. Enums are more type-safe than plain strings or numeric constants, and can hold fields and methods (Java 5 and later).

Syntax

// Basic enum definition.
enum Season {
    SPRING, SUMMER, AUTUMN, WINTER
}

// Enum definition with fields and methods.
enum Planet {
    MERCURY(3.303e+23, 2.4397e6),
    VENUS  (4.869e+24, 6.0518e6);

    private final double mass;
    private final double radius;

    Planet(double mass, double radius) {
        this.mass = mass;
        this.radius = radius;
    }

    double surfaceGravity() { return 6.67430e-11 * mass / (radius * radius); }
}

// Common methods.
Season[] all  = Season.values();          // Returns all enum constants as an array.
Season s      = Season.valueOf("SPRING"); // Returns the enum constant with the given name.
int idx       = Season.SUMMER.ordinal();  // Returns the zero-based declaration index.
String name   = Season.SUMMER.name();     // Returns the declaration name as a string.

Method List

MethodDescription
values()Returns an array of all enum constants in declaration order.
valueOf(String name)Returns the enum constant with the matching name. Throws IllegalArgumentException if no match is found.
ordinal()Returns the zero-based index of the enum constant in declaration order.
name()Returns the declaration name of the enum constant as a string. Same as the default behavior of toString().
compareTo(E other)Compares this enum constant with the specified constant by declaration order.

Sample Code

// Basic enum usage.
enum Day { MON, TUE, WED, THU, FRI, SAT, SUN }

Day today = Day.WED;
System.out.println(today);           // Prints "WED".
System.out.println(today.ordinal()); // Prints "2".
System.out.println(today.name());    // Prints "WED".

// Using an enum in a switch statement.
switch (today) {
    case SAT: case SUN:
        System.out.println("Weekend.");
        break;
    default:
        System.out.println("Weekday.");
}

// Iterating with values().
for (Day d : Day.values()) {
    System.out.print(d + " "); // Prints "MON TUE WED THU FRI SAT SUN ".
}

// Using an enum with fields and methods.
enum Status {
    PENDING("Pending"),
    ACTIVE("Active"),
    CLOSED("Closed");

    private final String label;
    Status(String label) { this.label = label; }
    String getLabel() { return label; }
}

System.out.println(Status.ACTIVE.getLabel()); // Prints "Active".

// Getting an enum constant from a string with valueOf().
Status s = Status.valueOf("CLOSED");
System.out.println(s.getLabel()); // Prints "Closed".

Notes

Enums are more type-safe than plain constants, preventing invalid assignments at compile time. You can add fields and methods to attach data and behavior to each enum constant.

Avoid using ordinal() values for database storage or other persistence — adding or reordering constants will break existing data. Use name() or a dedicated numeric field instead.

For immutable data classes, see 'record'. For details on access modifiers, see 'public / private / protected / static / final'.

If you find any errors or copyright issues, please .