Featured resource
2026 Tech Forecast
2026 Tech Forecast

Stay ahead of what’s next in tech with predictions from 1,500+ business leaders, insiders, and Pluralsight Authors.

Get these insights
  • Lab
    • Libraries: If you want this lab, consider one of these libraries.
    • Core Tech
Labs

Guided: Apply Records and Pattern Matching in Java Services

In this Code Lab, you'll step into the role of a developer tasked with modernizing a Java application. You will take verbose, boilerplate-heavy classes and refactor them into clean, immutable records. You'll also simplify complex conditional logic using the latest pattern matching features for both `instanceof` and `switch` statements, making the code more readable and robust.

Lab platform
Lab Info
Level
Intermediate
Last updated
Dec 06, 2025
Duration
45m

Contact sales

By clicking submit, you agree to our Privacy Policy and Terms of Use, and consent to receive marketing emails from Pluralsight.
Table of Contents
  1. Challenge

    Introduction

    Welcome to this Code Lab on modern Java features! You'll explore how records and pattern matching can significantly reduce boilerplate and improve the clarity and safety of your code. You'll start with a traditional Java application and refactor it step by step.

    Before you dive into writing new code, familiarize yourself with the project structure. You're working with a standard Maven project where the main logic lives in src/main/java and tests are in src/test/java. Explore the files, especially LegacyBook.java, which you'll refactor first, and MediaService.java, where you'll implement pattern matching.

    Navigation Tip: For easier code navigation, collapse the Workspace view in the Explorer pane and expand the Projects view instead. The Projects view organizes your Java files by package structure, making it much simpler to locate classes and interfaces.

    You'll see two main projects:

    • LAB-CODE: Your working code that you'll modify throughout the exercises
    • SOLUTION: Reference implementations for each step (files follow the pattern FILENAME-STEP#-TASK#.java, e.g., Book-2-1.java)

    Start by expanding LAB-CODE to see the source code you'll be working with. If you get stuck on any task, you can reference the corresponding solution file for guidance.

    info> 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.

  2. Challenge

    Step 2: From Verbose Classes to Concise Records

    A common task in Java is creating classes to simply hold data, like results from a database query or a network request. Historically, this meant writing a lot of boilerplate: private final fields, a constructor, accessor methods, and equals(), hashCode(), and toString() implementations. These classes are often called Plain Old Java Objects (POJOs) or data transfer objects (DTOs).

    Java records, introduced as a standard feature in Java 16, are designed to eliminate this ceremony. They are a special, more restricted kind of class, optimized for the role of being a simple, immutable data aggregate.

  3. Challenge

    Step 3: Building a Data Model with Records

    Now that you understand the basics of records, build out your data model. By using records for all your data-carrying objects, you ensure they are simple, immutable, and consistent. This also sets you up perfectly for using pattern matching in the next steps.

    You'll also use a sealed interface named Media. A sealed interface restricts which other classes or interfaces may extend or implement it. This gives the compiler a complete picture of the type hierarchy, which is essential for exhaustive checks in pattern matching.

  4. Challenge

    Step 4: Simplifying `instanceof` Checks

    A very common code pattern in Java is checking if an object is an instance of a specific type, and if so, casting it to that type to call one of its methods. This pattern is verbose and contains a redundancy: the type is mentioned twice.

    if (obj instanceof String) {
        String s = (String) obj;
        System.out.println(s.length());
    }
    

    Pattern matching for instanceof (a standard feature since Java 16) streamlines this. It lets you declare a new variable of the target type, which is only in scope if the check succeeds. This makes the code more concise and removes the need for the manual cast.

  5. Challenge

    Step 5: Advanced Data Handling with `switch` Expressions

    The final feature you'll explore is pattern matching for switch expressions, made standard in Java 21. This feature elevates switch from a simple control flow statement to a powerful tool for data-oriented programming. You can switch on an object and have case labels that match against types, not just constant values.

    When you use this with records and sealed interfaces, it allows for exhaustive and elegant handling of different data types. The compiler can verify that you've handled all permitted subtypes of a sealed interface, eliminating a whole class of runtime errors. -----

    Congratulations on completing the lab! You've successfully modernized a Java application using records and pattern matching.

    You've learned how to:

    • Replace verbose POJOs with concise record types for immutable data.
    • Simplify instanceof checks and casts using pattern matching.
    • Rewrite cumbersome if-else chains as elegant and type-safe switch expressions.

    These modern Java features help you write code that is not only shorter but also more readable, robust, and a pleasure to maintain.

About the author

Pluralsight Code Labs offer an opportunity to get hands-on learning in real-time. Be it a Challenge, Sandbox, or Guided Lab, these provide the real world experience needed to succeed in your development role.

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.

Get started with Pluralsight