Java Tutorial

Java Enums

An enum names a fixed set of constants so you write Day.MON instead of magic numbers — and the compiler rejects values that are not on the list.

Named constants

An enum is a type whose allowed values you list once. Use those names in variables, comparisons, andswitch instead of 0, 1, 2 — the names document intent and typos become compile errors.

Example

enum Day { MON, TUE, WED, THU, FRI }

public class Main {
  public static void main(String[] args) {
    Day d = Day.TUE;
    System.out.println(d);           // TUE
    System.out.println(d.ordinal()); // 1
  }
}

Compare and switch

Enum values compare with ==. A switch on an enum is especially clear: each case is a named constant, not a fragile number.

Example

enum Status { OPEN, CLOSED, PENDING }

public class Main {
  public static void main(String[] args) {
    Status s = Status.OPEN;
    switch (s) {
      case OPEN -> System.out.println("Accepting orders");
      case CLOSED -> System.out.println("Come back later");
      case PENDING -> System.out.println("Almost ready");
    }
  }
}

values and ordinal

Day.values() returns every constant in declaration order. ordinal() is the zero-based index. Prefer the name in your own logic — ordinals break if someone reorders the list.

Example

enum Size { S, M, L }

public class Main {
  public static void main(String[] args) {
    for (Size size : Size.values()) {
      System.out.println(size + " at " + size.ordinal());
    }
  }
}

Do not store ordinal() in a database as a permanent key. Persist the enum name (size.name()) or an explicit field so reordering constants does not corrupt old data.

Try It Yourself

Exercise: Define enum TrafficLight { RED, YELLOW, GREEN }. Set a variable to YELLOW and print a message for each color with if or switch.

Show solution
enum TrafficLight { RED, YELLOW, GREEN }

public class Main {
  public static void main(String[] args) {
    TrafficLight light = TrafficLight.YELLOW;
    if (light == TrafficLight.RED) System.out.println("Stop");
    else if (light == TrafficLight.YELLOW) System.out.println("Slow");
    else System.out.println("Go");
  }
}

The enum limits the variable to three values; comparing with == is the usual pattern.

Key Takeaways

  • An enum lists a fixed set of named constants as a type.
  • Use names in variables and switch instead of magic numbers.
  • values() iterates every constant; ordinal() is the index — prefer names.
  • Invalid values are rejected at compile time, not discovered later as a wrong int.

Worked examples

The short listings above are there so you can see the grammar. The programs here use the same statements on quantities that already have units: a speed, a pH, a count of bases. They are classroom numbers. Air resistance is ignored. g is 9.81 m/s² unless a line says otherwise.

Open them in the Java editor at /java/try. Change one measurement and check whether the result still has the right unit.

Chemistry

Named phase states

Solid, liquid, gas are a closed set. An enum names them so you cannot invent a fourth phase by typo.

Example

enum Phase { SOLID, LIQUID, GAS }

public class Main {
  public static void main(String[] args) {
    Phase p = Phase.LIQUID;
    System.out.println(p);
  }
}

Biology

Base enum

DNA uses four bases. Enum constants keep A, C, G, T as a type, not free strings.

Example

enum Base { A, C, G, T }

public class Main {
  public static void main(String[] args) {
    Base b = Base.G;
    System.out.println(b.ordinal());
  }
}

FAQ: Java Enums

Common questions about this page.

What is the StudyGrid Java tutorial?

The StudyGrid Java tutorial is a full beginner-to-advanced track: syntax, types, input, loops, methods, classes, collections, generics, maps, and lambdas. Each chapter has copy-and-run examples.

Should I run java enum examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn java enum in this Java Java lesson (Java Enums).

Is the Java editor the same as Try Python or Try C++?

No. Try Java compiles with javac at /java/try and shows stdout plus compiler messages. Try Python stays at /try. Try C++ stays at /cpp/try. Java lessons never open those editors.

Do I need to install a JDK to learn Java?

No. Open a chapter, click Try it in Java, and compile in the browser. You can also download a .java file and compile locally with javac.

Where should I start the Java tutorial?

Start at Java Intro, then Get Started and Syntax. After the first program, continue to output, variables, and if-else. After classes, open Java Examples, then generics, map, and lambdas. Use Next at the bottom of each chapter.

Is the Java tutorial free?

Yes. The Java workshop on StudyGrid (studygrid.in) is free: dashboard, chapters, and the compile-and-run editor.