java-21 — Claude Code skill: Java 21 patterns
Teaches the agent to write Java 21 code: records, sealed types, virtual threads. Useful once your project is already on 21.
npx skills add Gentleman-Programming/Gentleman-Skills --skill java-21 -a claude-codeWhat java-21 does
The skill loads when the agent writes Java 21 application or library code, designs immutable DTOs and value objects, models closed hierarchies, or handles blocking I/O. Inside are three patterns with code: a record that validates in its compact constructor, a sealed interface matched exhaustively with switch, and virtual threads via Executors.newVirtualThreadPerTaskExecutor().
A separate section covers anti-patterns. A mutable DTO with public fields and a raw new Thread(...) per request are both flagged as bad practice — the second one is expensive and unbounded. There is also a quick reference table mapping each task to its pattern.
The skill targets Java 21 specifically: records, sealed types and virtual threads are features of that release, so the examples will not compile on older JDKs. Worth checking which version your pom.xml or build.gradle pins before you install.
What it helps with
- write an immutable DTO that validates its own fields
- close a payment hierarchy with a sealed interface and match it in switch
- handle blocking I/O without a large thread pool
- replace a mutable data carrier class with a record
- stop spawning a new thread for every request
Example prompts for the agent
- Write an Email record that validates its value in the compact constructor
- Create a sealed Payment interface with Card and BankTransfer, then describe it with switch
- Rewrite this URL fetch loop to use virtual threads via Executors.newVirtualThreadPerTaskExecutor()
How to install java-21
1Skills CLI (Vercel)
npx skills add Gentleman-Programming/Gentleman-Skills --skill java-21 -a claude-code2Manually: put the files in place
~/.claude/skills/java-21/SKILL.mdCopy the whole skill folder; for one project use .claude/skills/java-21/
1Skills CLI (Vercel)
npx skills add Gentleman-Programming/Gentleman-Skills --skill java-21 -a codex2Manually: put the files in place
.agents/skills/java-21/SKILL.mdFor all projects use ~/.agents/skills/
1Skills CLI (Vercel)
npx skills add Gentleman-Programming/Gentleman-Skills --skill java-21 -a cursor2Manually: put the files in place
.cursor/skills/java-21/SKILL.mdCursor also reads .agents/skills/ and .claude/skills/
1GitHub CLI
gh skill install Gentleman-Programming/Gentleman-Skills java-212Manually: put the files in place
.github/skills/java-21/SKILL.md1Skills CLI (Vercel)
npx skills add Gentleman-Programming/Gentleman-Skills --skill java-21 -a gemini-cli2Platform CLI
gemini skills install https://github.com/Gentleman-Programming/Gentleman-Skills --path community/java-21Manually: put the files in place
.devin/skills/java-21/SKILL.mdThe legacy .windsurf/skills/ path still works
Skills CLI (Vercel)
npx skills add Gentleman-Programming/Gentleman-Skills --skill java-21 -a opencodeClaude.ai
Zip the folder and upload it in Settings → Capabilities → Skills
Step by step, with commands for each platform: How to install skills in Claude Code: five ways
Questions about java-21
How do I install java-21 in Claude?
Run npx skills add Gentleman-Programming/Gentleman-Skills --skill java-21 -a claude-code. The skill file lands at ~/.claude/skills/java-21/SKILL.md.
What does the java-21 skill do?
It hands the agent ready patterns for Java 21: records for immutable data, sealed types with switch pattern matching, virtual threads for blocking calls. It also lists anti-patterns to avoid.
Which Java versions does this skill cover?
Java 21. Records, sealed types and virtual threads come from that release, so the examples will not compile on older JDKs.
Source text
SKILL.md · MIT
---
name: java-21
description: >
Java 21 language and runtime patterns for modern, safe code.
Trigger: When writing Java 21 code using records, sealed types, or virtual threads.
metadata:
author: diegnghrmr
version: "1.0"
---
## When to Use
Load this skill when:
- Writing Java 21 application or library code
- Designing immutable DTOs or value objects
- Modeling closed hierarchies with sealed types
- Using virtual threads for blocking I/O
## Critical Patterns
### Pattern 1: Records for immutable data
Use records for DTOs and value objects, validate in compact constructors.
### Pattern 2: Sealed types + pattern matching
Use sealed interfaces/classes and switch pattern matching for exhaustiveness.
### Pattern 3: Virtual threads for I/O
Use virtual threads to handle blocking I/O without large thread pools.
## Code Examples
### Example 1: Record with validation
```java
package com.acme.user;
public record Email(String value) {
public Email {
if (value == null || !value.contains("@")) {
throw new IllegalArgumentException("Invalid email");
}
}
}
```
### Example 2: Sealed hierarchy + switch pattern matching
```java
package com.acme.payment;
public sealed interface Payment permits Card, BankTransfer { }
public record Card(String last4) implements Payment { }
public record BankTransfer(String iban) implements Payment { }
public final class PaymentPrinter {
public String describe(Payment payment) {
return switch (payment) {
case Card card -> "card-" + card.last4();
case BankTransfer bank -> "iban-" + bank.iban();
};
}
}
```
### Example 3: Virtual threads for blocking calls
```java
package com.acme.io;
import java.util.concurrent.Executors;
public final class Fetcher {
public void fetchAll(java.util.List<String> urls) throws Exception {
try (var executor = Executors.newVirtualThreadPerTaskExecutor()) {
for (String url : urls) {
executor.submit(() -> blockingFetch(url));
}
}
}
private void blockingFetch(String url) {
// perform blocking I/O here
}
}
```
## Anti-Patterns
### Don't: Use mutable data carriers
```java
// BAD: mutable DTO
public class UserDto {
public String name;
public String email;
}
```
### Don't: Spin up raw platform threads per request
```java
// BAD: expensive and unbounded
new Thread(() -> blockingFetch("https://api" )).start();
```
## Quick Reference
| Task | Pattern |
|------|---------|
| Immutable DTOs | Use records with validation |
| Closed hierarchies | Use sealed interfaces + switch |
| Blocking I/O scale | Use virtual threads executor |
## Resources
- Java 21 Release Notes: https://www.oracle.com/java/technologies/javase/21-relnote-issues.html
- JEP Index: https://openjdk.org/jeps/0
© diegnghrmr · MIT · Gentleman-Programming/Gentleman-Skills