Skip to main content
Module 8 · Lesson 1 of 217/22 in the course~16 min
Module lessons (1/2)

Records, Sealed Classes and Pattern Matching

Lesson preview5 hands-on exercises readyCourse Java · Module 8 — Java Modern: Record, Sealed and Optional

Immutable data carriers with record, compact constructor, closed hierarchies sealed ... permits and instanceof patterns.

What you'll learn in this lesson

  • Replace data/DTO boilerplate classes with immutable record, complete with equals, hashCode, and toString.
  • Validate invariants in the compact constructor while maintaining the superficial immutability of the record.
  • Restrict allowed subtypes using sealed / permits and extract fields without explicit casts using pattern matching (instanceof).

Overview

From Java 16 and 17 onwards, Record, Sealed Classes, and Pattern Matching transform data modeling in Java by making it concise and compiler-verified.

With a single line you define a data carrier with fields private final, canonical constructor, accessor methods (x(), y(), without prefix get), equals(), hashCode() and toString():

Sections covered in this lesson

  1. 11. Immutable Records and Compact Constructor
  2. 22. Closed Hierarchies with sealed and permits (Java 17+)
  3. 33. Pattern Matching for instanceof and switch (Java 16/21)
  4. 4Try it yourself
Pro Lesson · Module 8 — Java Modern: Record, Sealed and Optional

Unlock "Records, Sealed Classes and Pattern Matching" and the full Course Java curriculum

Immutable data carriers with `record`, compact constructor, closed hierarchies `sealed ... permits` and `instanceof` patterns.

Unlock all 5 hands-on exercises in this lesson plus every advanced WebAssembly module with automated checks.

  • Instant access to all advanced modules in Course Java plus 17 more courses
  • Hands-on browser exercises with automated test suites and solutions
  • 14-day money-back guarantee processed via Paddle.com

14-day guarantee · Cancel anytimeModule 1, all 18 interactive games, and cheatsheets are always free.