java

A set of coding rules for modern Java, the programming language, targeting Java 17 or later and including newer features available in Java 21. It guides choices such as immutable data records, sealed class hierarchies, streams, and virtual threads.

In plain words
What is it for?
Writing or reviewing Java 17+ code, choosing between records and regular classes, designing closed type hierarchies, creating text blocks and switch expressions, using optional return values correctly, and applying Java 21 features where appropriate.
Why use it?
It keeps Java code consistent with current language features and discourages designs that create unnecessary coupling or mutable state. The rules also encourage compiler-checked handling of related types and states.

Cursor rule

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

agentmods
npx agentmods add rules/nedcodes-ok/cursorrules-collection/java
Clone the repo
git clone --depth 1 https://github.com/nedcodes-ok/cursorrules-collection
Per session 1,083 This file is loaded in full into every session.
When invoked 1,083 The same file — it is already loaded in full.
Security scan A 0 findings. Scan, not verified.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5 $0.01083 $0.01083
Opus 5 $0.00541 $0.00541
Sonnet 5 $0.00217 $0.00217
Haiku 4.5 $0.00108 $0.00108

Measured yesterday against content hash 9cfe72f7a647, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade A, and why

java scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured yesterday.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

rules-mdc/languages/java.mdc · 55 lines

How it starts

The opening of the file, as written. The whole thing — 55 lines — stays where its author put it; the contents beside it link to each section on GitHub.

Java Cursor Rules

You are an expert Java developer (17+, preferably 21+). Follow these rules:

Modern Java Features

  • Target Java 17+ minimum: records, sealed classes, pattern matching for instanceof, text blocks, switch expressions
  • Java 21+: virtual threads, record patterns, sequenced collections, pattern matching for switch
  • record UserDTO(String name, String email) {} for immutable data carriers — replaces POJOs with equals/hashCode/toString generated. Don't add mutable fields or setters to records
  • sealed interface Shape permits Circle, Rectangle {} for closed type hierarchies — the compiler ensures all subtypes are handled in switch/pattern matching
  • Text blocks (""") for multi-line strings (SQL, JSON, HTML templates) — no more string concatenation hell
  • Switch expressions: var label = switch(status) { case ACTIVE -> "Active"; case INACTIVE -> "Disabled"; }; — exhaustiveness checked by compiler with sealed types

Design

  • Composition over inheritance — extends creates tight coupling. Use interfaces + delegation
  • Optional<T> for return types that may be absent. Never use Optional as a method parameter, field, or collection element — it's a return type signal, not a general-purpose wrapper
  • Immutable by default: final fields, no setters, constructor initialization. Use record or manually immutable classes. Mutable state is the source of most concurrency bugs
  • Dependency injection via constructor, not field injection (@Autowired on fields) — constructor injection is explicit, testable, and fails fast on missing dependencies

Collections & Streams

  • List.of(), Map.of(), Set.of() for immutable collections — they throw on modification attempts. List.copyOf() for defensive copies
  • Stream API for transformations, keep chains to 4-5 operations max. Beyond that, extract intermediate variables or helper methods — unreadable streams are worse than loops
  • var for local variables when the type is obvious from the right side: var users = repository.findAll(). Don't use var when the type isn't clear: var result = process(data) — what's result?
  • EnumMap / EnumSet for enum-keyed collections — faster and more memory-efficient than HashMap/HashSet with enum keys
  • toList() (Java 16+) over .collect(Collectors.toList()) — shorter and returns an unmodifiable list

Read the full file on GitHub · 55 lines

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. yesterday First seen · 55 lines · 1,083 tokens per session scan A 9cfe72f7a647

Subscribe to this mod's changes

java is a cursor rule published in the GitHub repository nedcodes-ok/cursorrules-collection (37 stars, last pushed 6mo ago), licensed MIT. It adds 1,083 tokens to every session, about $0.0054 per session on Opus 5. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-30.