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
| Feature | Java Version | Benefit |
|---|---|---|
| Records | 16+ | Immutable data carriers |
| Sealed Classes | 17+ | Controlled inheritance |
| Pattern Matching | 17-21 | Exhaustive type handling |
| Text Blocks | 15+ | Readable multiline strings |
| Switch Expressions | 14+ | 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)CodeLoading syntax highlighter...
Pattern 2: Records for DTOs
JAVA(55 lines)CodeLoading syntax highlighter...
Pattern 3: Sealed Classes for Domain Modeling
JAVA(70 lines)CodeLoading syntax highlighter...
Pattern 4: Pattern Matching with Switch
JAVA(46 lines)CodeLoading syntax highlighter...
Pattern 5: Text Blocks for Queries and Templates
JAVA(81 lines)CodeLoading syntax highlighter...
Pattern 6: Enhanced Switch Expressions
JAVA(43 lines)CodeLoading syntax highlighter...
Pattern 7: Virtual Threads (Java 21)
JAVA(39 lines)CodeLoading syntax highlighter...
Pattern 8: Migrating to Records
JAVA(60 lines)CodeLoading 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)CodeLoading 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:
JAVACodeLoading syntax highlighter...
The
List inside the record can be modified by anyone who has a reference to it:JAVACodeLoading syntax highlighter...
Since the same
OrderDto is cached, modifications by OrderPrinter corrupt the cached instance for all subsequent users.Correct implementation:
JAVA(52 lines)CodeLoading 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)CodeLoading syntax highlighter...
Requirements: Validate required fields, ensure roles is immutable.
✅ Solution:
JAVA(49 lines)CodeLoading syntax highlighter...
Exercise 2: Sealed Class Hierarchy
⭐⭐ Difficulty: Medium | ⏱️ Time: 15 minutes
Task: Model a notification system using sealed classes.
JAVA(5 lines)CodeLoading syntax highlighter...
✅ Solution:
JAVA(117 lines)CodeLoading syntax highlighter...
Exercise 3: Pattern Matching Switch
⭐⭐ Difficulty: Medium | ⏱️ Time: 15 minutes
Task: Implement a payment fee calculator using pattern matching.
JAVA(6 lines)CodeLoading syntax highlighter...
✅ Solution:
JAVA(92 lines)CodeLoading 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)CodeLoading syntax highlighter...
Requirements: Use text blocks, named parameters, and prevent SQL injection.
✅ Solution:
JAVA(124 lines)CodeLoading 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)CodeLoading syntax highlighter...
✅ Solution:
JAVA(200 lines)CodeLoading syntax highlighter...
📝 Summary
| Feature | Use When |
|---|---|
| Records | Immutable data (DTOs, value objects) |
| Sealed Classes | Fixed set of subtypes |
| Pattern Matching | Type-based dispatch |
| Text Blocks | SQL, JSON, HTML, multiline strings |
| Switch Expressions | Returning values from conditionals |
| Virtual Threads | High-concurrency I/O |
📅 Review Schedule for This Article
| Day | Task | Time |
|---|---|---|
| Day 1 | Review Records vs Classes decision table | 5 min |
| Day 3 | Redo Exercise 1 (Convert to Record) | 10 min |
| Day 7 | Convert one DTO in your codebase to a record | 15 min |
| Day 14 | Redo Debug This (The Leaky Record) | 10 min |
| Day 30 | Design a sealed class hierarchy for a domain in your project | 25 min |
Next: [Part 24: Clean Code in Spring Boot]