Enums and Records
LockedLesson 19 of 60 · OOP · Medium
Finish these first:Constructors and Encapsulation →Or pass the tests below to unlock it now.
The big idea
enum models a fixed set of constants (can have fields and methods). record is a concise immutable data carrier.
Think of it like this: Think of a Class as a cookie cutter (the blueprint) and each Object as an individual cookie stamped from it — each cookie can have its own frosting (state) while sharing the same shape (methods).
Read the lesson
enum models a fixed set of constants (can have fields and methods). record is a concise immutable data carrier.
enum Level { LOW, HIGH }
record Point(int x, int y) {
Point { if (x < 0) throw new IllegalArgumentException(); }
}
Quick check
Before writing code: What should `Solution.manhattan(new Solution.Point(0,0), new Solution.Point(3,4))` return when your implementation of Enums and Records is working?
Your challenge
Define record Point(int x, int y) and static int manhattan(Point a, Point b).
More learning tools(compare languages, visual guide, related lessons)
Compare with another language
Similar to python.dataclasses-typing (70% overlap). `@dataclass(frozen=True)` ~ Java `record`.
Visual walkthrough
A diagram, a step-by-step animation, and short captions for Enums and Records.
Learn this first
Opens up next
This is the last lesson in this branch.