0Pricing
Java Academy · Lesson

Defining and Using Enums

Declare enum types, use their constants, and iterate with values() and ordinal().

Defining and Using Enums

An enum (enumeration) is a special class that represents a fixed set of constants. Enums are type-safe, readable, and more powerful than static int constants.

Declaring an Enum

Use the enum keyword to declare an enumeration. Each constant is implicitly public static final and an instance of the enum type.

enum Day {
    MONDAY, TUESDAY, WEDNESDAY, THURSDAY,
    FRIDAY, SATURDAY, SUNDAY
}

Day today = Day.WEDNESDAY;
System.out.println(today);          // WEDNESDAY
System.out.println(today.name());   // WEDNESDAY
System.out.println(today.ordinal()); // 2 (zero-based index)

All lessons in this course

  1. Defining and Using Enums
  2. Enums with Fields and Methods
  3. Switch Expressions with Enums
  4. EnumSet and EnumMap
← Back to Java Academy