Lesson 1 of 514 minModule progress 0%

Module 14: Enums and Annotations

Enum Basics

Use enums for fixed sets of values instead of fragile strings or magic numbers.

Author

Java Learner Editorial Team

Reviewer

Technical review by Java Learner

Last reviewed

2026-04-17

Java version

Java 25 LTS

How this lesson was prepared: AI-assisted draft, manually edited for clarity, and checked against current Java documentation and runnable examples.

Learning goals

  • Know when an enum is better than a string constant
  • Use enum values safely in code and `switch` statements
  • Understand enum instances as real typed values

Enums represent a fixed set of valid values: Days, statuses, difficulty levels, and workflow steps are common examples.

They are safer than raw strings: You avoid misspellings like "PENDNG" and get compiler support when you switch over values.

Enums are real types, not just labels: They can be stored, compared, and passed around with meaning.

Practical upgrade: When you see a tiny domain with a closed value set, consider an enum before inventing ad hoc string constants.

Runnable examples

Status values as a typed enum

enum Status {
    NEW, IN_PROGRESS, DONE
}

Status status = Status.IN_PROGRESS;
System.out.println(status);

Expected output

IN_PROGRESS

Mini exercise

Create an enum for course difficulty with `BEGINNER`, `INTERMEDIATE`, and `ADVANCED`.

Summary

  • Enums model closed sets of valid values.
  • They are safer and clearer than loose string constants.
  • They work especially well for statuses and categories.

Next step

Next, add behavior and data to enums instead of treating them as plain labels only.

Sources used

Advertisement

Lesson check

Why are enums often better than raw strings for fixed statuses?

Next lesson →