Java

Modern Java Features

Java 17-21 introduced features that dramatically improve code clarity. This article covers Records, Sealed Classes, Pattern Matching, and other modern features that make clean code easier to write.

📋 At a Glance

FeatureJava VersionBenefit
Records16+Immutable data carriers
Sealed Classes17+Controlled inheritance
Pattern Matching17-21Exhaustive type handling
Text Blocks15+Readable multiline strings
Switch Expressions14+Concise conditionals

🎯 What You'll Learn

  • Records for DTOs and value objects
  • Sealed classes for domain modeling
  • Pattern matching for type-safe dispatch
  • Text blocks for SQL and JSON
  • Migrating legacy code to modern Java

🔬 Deep Dive

Pattern 1: Records for Value Objects

JAVA(69 lines)
Code
Loading syntax highlighter...

Pattern 2: Records for DTOs

JAVA(55 lines)
Code
Loading syntax highlighter...

Pattern 3: Sealed Classes for Domain Modeling

JAVA(70 lines)
Code
Loading syntax highlighter...

Pattern 4: Pattern Matching with Switch

JAVA(46 lines)
Code
Loading syntax highlighter...

Pattern 5: Text Blocks for Queries and Templates

JAVA(81 lines)
Code
Loading syntax highlighter...

Pattern 6: Enhanced Switch Expressions

JAVA(43 lines)
Code
Loading syntax highlighter...

Pattern 7: Virtual Threads (Java 21)

JAVA(39 lines)
Code
Loading syntax highlighter...

Pattern 8: Migrating to Records

JAVA(60 lines)
Code
Loading syntax highlighter...

🐛 Debug This: The Leaky Record

A developer converted their DTOs to Java records for "guaranteed immutability." But integration tests are failing with mysterious state changes. "Records are immutable! How can the order items be changing?"

JAVA(63 lines)
Code
Loading syntax highlighter...
The cached OrderDto is getting corrupted. Tests fail intermittently. What's wrong?

✅ Solution:
The record contains a mutable List - records only make their component references final, not the contents of those references!
Problem:
JAVA
Code
Loading syntax highlighter...
The List inside the record can be modified by anyone who has a reference to it:
JAVA
Code
Loading syntax highlighter...
Since the same OrderDto is cached, modifications by OrderPrinter corrupt the cached instance for all subsequent users.
Correct implementation:
JAVA(52 lines)
Code
Loading syntax highlighter...
The lesson: Records make component references immutable, not the objects they reference. Always use List.copyOf() or List.of() in record constructors for true immutability.

💻 Exercises

Exercise 1: Convert to Record

⭐ Difficulty: Easy | ⏱️ Time: 10 minutes

Task: Convert this POJO to a proper record with validation.
JAVA(20 lines)
Code
Loading syntax highlighter...
Requirements: Validate required fields, ensure roles is immutable.
✅ Solution:
JAVA(49 lines)
Code
Loading syntax highlighter...

Exercise 2: Sealed Class Hierarchy

⭐⭐ Difficulty: Medium | ⏱️ Time: 15 minutes

Task: Model a notification system using sealed classes.
JAVA(5 lines)
Code
Loading syntax highlighter...
✅ Solution:
JAVA(117 lines)
Code
Loading syntax highlighter...

Exercise 3: Pattern Matching Switch

⭐⭐ Difficulty: Medium | ⏱️ Time: 15 minutes

Task: Implement a payment fee calculator using pattern matching.
JAVA(6 lines)
Code
Loading syntax highlighter...
✅ Solution:
JAVA(92 lines)
Code
Loading syntax highlighter...

Exercise 4: Text Block SQL Repository

⭐⭐⭐ Difficulty: Medium-Hard | ⏱️ Time: 20 minutes

Task: Refactor this repository to use text blocks and modern Java features.
JAVA(30 lines)
Code
Loading syntax highlighter...
Requirements: Use text blocks, named parameters, and prevent SQL injection.
✅ Solution:
JAVA(124 lines)
Code
Loading syntax highlighter...

Exercise 5: Virtual Threads with Structured Concurrency

⭐⭐⭐⭐ Difficulty: Hard | ⏱️ Time: 25 minutes

Task: Implement a product enrichment service using virtual threads and structured concurrency.
JAVA(6 lines)
Code
Loading syntax highlighter...
✅ Solution:
JAVA(200 lines)
Code
Loading syntax highlighter...

📝 Summary

FeatureUse When
RecordsImmutable data (DTOs, value objects)
Sealed ClassesFixed set of subtypes
Pattern MatchingType-based dispatch
Text BlocksSQL, JSON, HTML, multiline strings
Switch ExpressionsReturning values from conditionals
Virtual ThreadsHigh-concurrency I/O

📅 Review Schedule for This Article

DayTaskTime
Day 1Review Records vs Classes decision table5 min
Day 3Redo Exercise 1 (Convert to Record)10 min
Day 7Convert one DTO in your codebase to a record15 min
Day 14Redo Debug This (The Leaky Record)10 min
Day 30Design a sealed class hierarchy for a domain in your project25 min

Next: [Part 24: Clean Code in Spring Boot]