Record Patterns
Destructure records in patterns.
Record Patterns is a free Java Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Record Patterns
A record pattern matches a record and destructures it, binding its components to variables in one step. No more calling each accessor by hand.
A Record to Destructure
Start with a simple record. Its components are what a pattern can extract.
public class Main {
record Point(int x, int y) {}
public static void main(String[] args) {
Point p = new Point(3, 4);
System.out.println(p.x() + ", " + p.y());
}
}Destructuring with instanceof
Instead of instanceof Point p then p.x(), write instanceof Point(int x, int y) to bind x and y directly.
public class Main {
record Point(int x, int y) {}
public static void main(String[] args) {
Object obj = new Point(3, 4);
if (obj instanceof Point(int x, int y)) {
System.out.println("Sum: " + (x + y));
}
}
}Components Become Locals
The names inside the pattern (x, y) are fresh local variables holding the record's component values. You choose the names; they need not match the record.
public class Main {
record Point(int x, int y) {}
public static void main(String[] args) {
Object obj = new Point(10, 20);
if (obj instanceof Point(int a, int b)) {
System.out.println("a=" + a + " b=" + b);
}
}
}Using var in Patterns
You can write var for a component to let the compiler infer its type, which is handy when types are long.
public class Main {
record Pair(String key, Integer value) {}
public static void main(String[] args) {
Object obj = new Pair("age", 30);
if (obj instanceof Pair(var key, var value)) {
System.out.println(key + " = " + value);
}
}
}Patterns in switch
Record patterns also work in switch, making case branches read like data shapes.
public class Main {
record Point(int x, int y) {}
static String classify(Object obj) {
return switch (obj) {
case Point(int x, int y) when x == y -> "on diagonal";
case Point(int x, int y) -> "at " + x + "," + y;
default -> "not a point";
};
}
public static void main(String[] args) {
System.out.println(classify(new Point(5, 5)));
System.out.println(classify(new Point(1, 2)));
}
}Type Inference Match
The component types in the pattern must be compatible with the record's declared types. Using the exact type or var is the safe choice.
Combining With Guards
Add a when guard after a record pattern to match only when the destructured values satisfy a condition.
public class Main {
record Temperature(double celsius) {}
static String alert(Object o) {
if (o instanceof Temperature(double c) && c > 38.0) {
return "fever: " + c;
}
return "normal";
}
public static void main(String[] args) {
System.out.println(alert(new Temperature(39.5)));
System.out.println(alert(new Temperature(36.6)));
}
}Cleaner Data Access
Record patterns shine when you only need a couple of fields. They eliminate a chain of accessor calls and make the relevant data obvious at the match site.
Null and Record Patterns
Like all instanceof patterns, a record pattern is false for null. The destructuring only happens once the value is confirmed to be a non-null instance of the record.
A Practical Example
Destructure to compute a value directly from the components.
public class Main {
record Rectangle(int width, int height) {}
static int area(Object o) {
if (o instanceof Rectangle(int w, int h)) {
return w * h;
}
return 0;
}
public static void main(String[] args) {
System.out.println(area(new Rectangle(4, 5)));
System.out.println(area("not a rect"));
}
}Quick Check
Test your understanding of record patterns.
Recap
You learned record patterns.
- They test for a record type and destructure its components at once.
- Component names are fresh locals you choose;
varinfers types. - They work in both
instanceofandswitch. - Like all patterns, they are false for null.
Frequently asked questions
Is the “Record Patterns” lesson free?
Yes — the full text of “Record Patterns” is free to read here on the web, and the Java Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.
What will I learn in “Record Patterns”?
Destructure records in patterns. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Java Academy?
No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Record Patterns” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Java Academy lesson?
Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.