Module lessons (1/2)
Records, Sealed Classes and Pattern Matching
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 withequals,hashCode, andtoString. - Validate invariants in the compact constructor while maintaining the superficial immutability of the record.
- Restrict allowed subtypes using
sealed/permitsand 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
- 11. Immutable Records and Compact Constructor
- 22. Closed Hierarchies with
sealedandpermits(Java 17+) - 33. Pattern Matching for
instanceofandswitch(Java 16/21) - 4Try it yourself
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.