Enums in Java
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.
Declaring an enum
enum Day { MONDAY, TUESDAY, ... } defines a fixed, exhaustive set of named constant values.
Genuine type safety
A variable or parameter typed as an enum can only ever hold one of its declared constants, checked at compile time.
Enums can carry their own data
Fields, constructors, and methods work just like an ordinary class, letting each constant have its own associated values.
Built-in methods
Every enum automatically gets values(), all constants as an array, and valueOf(String), looking one up by name, for free.
Switch statements over enums
The compiler can verify every declared constant is actually handled, catching a missed case at compile time.
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); }}Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in Java yourself — building beats watching.