- Lab
-
Libraries: If you want this lab, consider one of these libraries.
- Core Tech
Guided: Records and Pattern Matching in Java SE Apps
This lab modernizes a Java notification service by refactoring traditional classes to records, implementing a sealed interface for type safety, and replacing conditional checks with pattern matching in switch statements.
Lab Info
Table of Contents
-
Challenge
Step 1: Getting Started
Welcome to Records and Pattern Matching in Java SE Apps. This lab modernizes a notification processing application using modern Java features: records, sealed interfaces, and pattern matching.
The starter code contains traditional Java classes with verbose boilerplate. By the end, the code will be concise, type-safe, and expressive. > This lab experience was developed by the Pluralsight team using Forge, an internally developed AI tool utilizing Gemini technology. All sections were verified by human experts for accuracy prior to publication. For issue reporting, please contact us.
-
Challenge
Step 2: Converting Classes to Records
Java Records provide a compact syntax for immutable data classes. A record automatically generates:
- A canonical constructor
- Accessor methods (e.g.,
recipient()instead ofgetRecipient()) equals(),hashCode(), andtoString()methods
This step converts all three notification classes to records.
-
Challenge
Step 3: Creating a Sealed Type Hierarchy
Sealed interfaces restrict which classes can implement them. This creates a closed type hierarchy that the compiler can reason about.
With a sealed interface, the compiler knows all possible implementations, enabling exhaustive pattern matching without a default case.
-
Challenge
Step 4: Pattern Matching with Switch
Pattern matching for switch allows switching directly on object types. Combined with sealed interfaces, it provides:
- Type-safe handling of all cases
- Concise, readable code
- Compiler-verified exhaustiveness
This step replaces the verbose if-else chain with an elegant switch expression.
-
Challenge
Step 5: See It in Action
The refactoring is complete. The notification service now uses:
- Records for concise, immutable data classes
- Sealed interfaces for a closed type hierarchy
- Pattern matching for elegant, type-safe processing
Run the demo to see the modernized code in action. Congratulations — you've finished the lab! By completing these steps you modernized a notification service using three powerful Java features: records, sealed interfaces, and pattern matching. These changes eliminated boilerplate code, enabled compile-time type safety, and replaced verbose if-else chains with concise switch expressions.
About the author
Real skill practice before real-world application
Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.
Learn by doing
Engage hands-on with the tools and technologies you’re learning. You pick the skill, we provide the credentials and environment.
Follow your guide
All labs have detailed instructions and objectives, guiding you through the learning process and ensuring you understand every step.
Turn time into mastery
On average, you retain 75% more of your learning if you take time to practice. Hands-on labs set you up for success to make those skills stick.