Enums and Wrapper Classes in Java Explained
Learn Java enums and wrapper classes — use enums for a fixed set of safe values, understand Integer/Double wrappers, autoboxing, and handy methods like parseInt.
Two practical, everyday topics in this lesson. First, enums — a clean way to represent a fixed set of named values, like the days of the week or the status of an order. Second, wrapper classes — the objects that let primitives like int play nicely with collections and generics. Both are small, both are used constantly, and both will make your code clearer and safer.
Part 1: Enums
An enum (short for "enumeration") is a special type for a value that can only be one of a fixed set of options. Think of a traffic light: it's RED, YELLOW, or GREEN — never anything else. Enums let you say exactly that.
The problem enums solve
Without enums, people often use plain strings or numbers to represent status — which invites typos and invalid values:
String status = "ACTVE"; // typo! nothing stops this — a bug waiting to happen
The enum solution
enum Status { ACTIVE, INACTIVE, PENDING // the ONLY allowed values } Status s = Status.ACTIVE; // ✓ valid Status bad = Status.ACTVE; // ✗ COMPILE ERROR — typo caught instantly
Now only the three defined values are possible, and any typo is caught by the compiler. Enums work beautifully with switch, too:
switch (s) { case ACTIVE -> System.out.println("Up and running"); case INACTIVE -> System.out.println("Switched off"); case PENDING -> System.out.println("Waiting..."); }
This website uses an enum exactly like this for a blog post's status — DRAFT, PUBLISHED, SCHEDULED — which is far safer than juggling loose strings. Enums make "impossible" states genuinely impossible.
Part 2: Wrapper classes
Remember from the generics lesson that you can't write List<int>? That's because collections and generics work with objects, but int, double, boolean etc. are primitives, not objects. Wrapper classes are the object versions of each primitive:
| Primitive | Wrapper class |
|---|---|
int |
Integer |
double |
Double |
boolean |
Boolean |
char |
Character |
That's why a list of numbers is List<Integer>, not List<int>.
Autoboxing — Java does the conversion for you
The good news: Java automatically converts between a primitive and its wrapper, so you rarely think about it. This is called autoboxing (primitive → wrapper) and unboxing (wrapper → primitive):
List<Integer> nums = new ArrayList<>(); nums.add(5); // autoboxing: int 5 → Integer automatically int first = nums.get(0); // unboxing: Integer → int automatically
Bonus: handy wrapper methods
Wrapper classes also carry useful static helpers — you've likely already used one to turn text into a number:
int n = Integer.parseInt("42"); // String "42" → int 42 double d = Double.parseDouble("3.14"); // String → double int max = Integer.MAX_VALUE; // the biggest possible int
Enums are one of those features I wish I'd used sooner. Early on I represented statuses with strings and spent real time chasing bugs caused by a mistyped "Publishd". Switching to an enum made those bugs literally impossible — the compiler won't let a wrong value through. And once you know wrapper classes exist, the whole "why is it List<Integer> and not List<int>?" question answers itself.
Key takeaways
- An enum defines a fixed set of named constants — safer than loose strings, great with
switch. - Wrapper classes (
Integer,Double…) are the object versions of primitives. - Collections/generics need wrappers, e.g.
List<Integer>— and autoboxing converts automatically. - Wrappers offer handy helpers like
Integer.parseInt().
← Previous: Lesson 23 — Generics in Java
Next: Lesson 25 — equals() and hashCode() →