0Pricing
Java Academy · Lesson

Record Patterns

Destructure records in patterns.

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());
    }
}

All lessons in this course

  1. Pattern Matching for instanceof
  2. Record Patterns
  3. Nested Record Patterns
  4. Patterns in switch
← Back to Java Academy