Java Enums

</>Coding Tamilan.← Java course & curriculum
Java Masterclass · Lesson 37 / 50 · English

Java Enums

Represent a fixed set of named choices.

Step-by-step explanationsRunnable JavaPractice + answer

Read this topic in தமிழ் ↗.

By the end of this lesson, you can…

  • Explain the key idea behind Enums.
  • Read and run a complete example, then predict what changes.
  • Recognize the topic-specific mistake discussed below.
01 / The concept

Understand the idea

An enum models a closed set of constants, such as the states in a simple workflow. Compared with arbitrary strings, it gives the compiler a known set of values. Enum names are identifiers; display text can be handled separately.

02 / Step by step

Explore the details

Enums

An enum is a special "class" that represents a group of constants (unchangeable variables, like final variables).

To create an enum, use the enum keyword (instead of class or interface), and separate the constants with a comma. Note that they should be in uppercase letters:

Example

Java reference fragmentJAVA
Level myVar = Level.MEDIUM;

Enum is short for "enumerations", which means "specifically listed".

Enum inside a Class

You can also have an enum inside a class:

Enum in a Switch Statement

Enums are often used in switch statements to check for corresponding values:

Loop Through an Enum

The enum type has a values() method, which returns an array of all enum constants. This method is useful when you want to loop through the constants of an enum:

Java reference fragmentJAVA
enum Day { MON, TUE, WED, THU, FRI, SAT, SUN }

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

// Enum in switch
switch (today) {
    case SAT: case SUN:
        System.out.println("Weekend!"); break;
    default:
        System.out.println("Weekday");
}
// Output: Weekday

// Loop through all enum values
for (Day d : Day.values()) {
    System.out.print(d + " ");
}
// MON TUE WED THU FRI SAT SUN

Difference between Enums and Classes An enum can, just like a class, have attributes and methods. The only difference is that enum constants are public, static and final (unchangeable – cannot be overridden). An enum cannot be used to create objects, and it cannot extend other classes (but it can implement interfaces). Why And When To Use Enums? Use enums when you have values that you know aren't going to change, like month days, days, colors, deck of cards, etc.

03 / Read and predict

A complete runnable example

This complete Main.java program focuses on the lesson idea. Reference fragments above may demonstrate individual statements; this example includes the imports, entry point, and supporting types it needs.

Main.java · complete runnable exampleJAVA
public class Main {
    public static void main(String[] args) throws Exception {
        Status status = Status.IN_PROGRESS;
        System.out.println(status);
        System.out.println(status == Status.IN_PROGRESS);
    }
}
enum Status { NEW, IN_PROGRESS, DONE }
Predict first. Then reveal the expected output / வெளியீடு
IN_PROGRESS
true

The output shown was checked by running this complete example. Trace the executed statements in order, then compare with your run.

04 / Learn by changing

Change it. Run it. Understand it.

Load this lesson example into the editor. Change one value or statement, predict the result, then press RUN to execute Java.

Open Java editor ↗
After the editor loads, choose Load this lesson example, then press RUN.

Powered by OneCompiler. Code you run is sent to OneCompiler for execution. For Scanner examples, supply input in the editor I/O panel; for file examples, files exist in its execution environment.

05 / Your turn

Make it your own

Practice challenge

Change status to DONE and predict the equality result.

Reveal the answer and reasoning / விடை

The first line becomes DONE, and the comparison becomes false.

One mistake to avoid: Avoid coupling stored business data to ordinal positions. Inserting an enum constant can change those positions.

Your learning checkpoint

  • Explain the example without reading the output first.
  • Make the change requested in the practice task.
  • Describe why the result changed—or why compilation failed.

Ready to move on? Follow the next lesson in course order, or return to the curriculum to review a prerequisite.

Coding Tamilan · Understand the idea. Try the code. Build the habit.