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
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