Learn Java
import com.learnjava.oopadvanced.Enums;

Enums in Java

@Concept

An enum is a special class type that represents a fixed, known set of named constants — enum Day { MONDAY, TUESDAY, WEDNESDAY, ... } declares exactly seven possible Day values, and no other Day value can ever exist. Unlike a plain set of int or String constants, an enum is genuinely type-safe: a method parameter typed as Day can only ever receive one of those seven declared values, which the compiler enforces, catching an invalid value at compile time instead of allowing some arbitrary string or number to sneak through unchecked.

An enum can have its own fields, constructors, and methods just like an ordinary class, letting each constant carry associated data — a Planet enum could give each planet constant its own mass and radius. A switch statement also works especially cleanly over an enum, since the compiler can verify every possible value has actually been handled.

@HowItWorks
1

Declaring an enum

enum Day { MONDAY, TUESDAY, ... } defines a fixed, exhaustive set of named constant values.

2

Genuine type safety

A variable or parameter typed as an enum can only ever hold one of its declared constants, checked at compile time.

3

Enums can carry their own data

Fields, constructors, and methods work just like an ordinary class, letting each constant have its own associated values.

4

Built-in methods

Every enum automatically gets values(), all constants as an array, and valueOf(String), looking one up by name, for free.

5

Switch statements over enums

The compiler can verify every declared constant is actually handled, catching a missed case at compile time.

@Example
public class EnumDemo {
enum Day { MONDAY, TUESDAY, WEDNESDAY }
 
public static void main(String[] args) {
Day today = Day.TUESDAY;
String result = switch (today) {
case MONDAY -> "Start of the week";
case TUESDAY -> "Getting into it";
case WEDNESDAY -> "Halfway there";
};
System.out.println(result);
}
}
Output
Getting into it
1today is declared as a Day, one of exactly three possible values this enum defines.
2The switch expression compares today against each declared constant in turn.
3case TUESDAY matches, since today holds exactly that value, so the expression evaluates to "Getting into it".
4The single println call prints that matched result as one line.
@UseCases
Representing a fixed set of known options, like days of the week or card suits, with genuine type safety
Replacing a set of loosely-related int or String constants with a properly checked enum type
Attaching data to each constant, like giving each Planet enum value its own mass
Letting the compiler confirm every possible enum value is handled in a switch

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Java yourself — building beats watching.