java-best-practices
Skill1 starsChanged 3 months agoMITView on GitHub
Use when writing, modifying, debugging, or reviewing Java source or tests, or answering questions about Java behavior. Relevant .java files can establish the language when the request omits it. Covers Google Java formatting, comments, JDK compatibility, and defensive coding. Skip build-only tasks.
5 other projects use a version of this.
Adopted by
Most recently taken up by
- baekenough/oh-my-customcodetheir own version, first seen Nov 23, 2026
- shivangiG/CodeAtlastheir own version, first seen May 3, 2026
- eggboy/skillstheir own version, first seen Apr 3, 2026
- senoritadeveloper01/claude-skillstheir own version, first seen Mar 24, 2026
- VirtusLab/agent-skillstheir own version, first seen Mar 5, 2026
Other versions5 projects use a version of this.
Word for word · 0
None found.
Edited · 0
None found.
Wrote their own version · 5
- baekenough/oh-my-customcode★ 47same name
- VirtusLab/agent-skills★ 7same name
- senoritadeveloper01/claude-skills★ 4same name
- shivangiG/CodeAtlas★ 1same name
- eggboy/skills★ 0same name
How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.
In this repository11 other files it gives its agents.
- AGENTS.mdAGENTS.md
- CLAUDE.mdCLAUDE.md
- ai-learning-coachSkill
- china-travel-plannerSkill
- codebase-readerSkill
- full-bleed-icon-creatorSkill
- git-commit-push-zhSkill
- github-trending-reportSkill
- session-cleanupSkill
- social-video-archiverSkill
- video-to-cover-briefSkill
The file
Show its rules and text
We split this skill into the rules it gives an agent
18 rules. 0 are common, 13 are distinctive, and 5 are very specific to this project.
0
Common
Most of the 5 edited versions we read keep these.
13
Distinctive
This author's own choices. Worth a look.
5
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 14
Use the Google Java Style Guide for style, the Alibaba Java Development Manual for defensive coding, and the target JDK's contracts for …
- Distinctive: line 31
Read the applicable repository instructions, nearby code, and target module's build settings.
- Distinctive: line 32
Check the compiler release or, where set separately, the source and target levels.
- Distinctive: line 46
Treat names and existing comments as leads to verify.
- Distinctive: line 47
When evidence is missing, narrow the claim or mark it unverified.
- Distinctive: line 60
Read every reference section that applies, including when one expression involves several contracts (for example, decimal equality in a map …
- Distinctive: line 77
Use early returns or continues for exceptional cases.
- Distinctive: line 80
Follow explicit user and repository rules.
- Very specific: line 81
Use / ... */ Javadoc for class, field, and method declaration comments, including non-public members.
- Very specific: line 84
Put implementation-specific rationale beside the relevant statements inside the method, using // or /* ...
- Distinctive: line 89
Document units, nullability, and boundaries of public and protected APIs in Javadoc.
- Very specific: line 91
Add @author and @since when the project's or user-designated reference's convention calls for them.
- Distinctive: line 94
Ask only when a required value cannot be established.
- Very specific: line 95
Use explicit imports, conventional modifier order, braces around control-flow bodies, and uppercase L for long literals.
- Very specific: line 104
Use the repository's configured google-java-format task or pinned formatter within that scope.
- Distinctive: line 117
Check new, edited, and affected existing comments against the inspected behavior and the project's form, placement, language, and …
- Distinctive: line 131
When changing this skill's behavior, use the isolated cases in evals/README.md.
- Distinctive: line 132
Keep evaluation prompts and grading criteria out of ordinary Java task context.
“Common” is measured: kept by at least 6 in 10 of the 5 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Java Best Practices
Overview
Use the Google Java Style Guide for style, the Alibaba Java Development Manual for defensive coding, and the target JDK's contracts for runtime behavior. For attribution or conflicting guidance, consult the source notes.
When to Use
- For implementation, debugging, and Java comment or Javadoc edits, follow the target module's conventions and limit edits to the requested scope. Standalone documentation, build, and deployment tasks are outside scope.
- For a review, keep the worktree read-only and apply the rules relevant to the diff.
- For a Java behavior question, go directly to the relevant contract. Ask for the JDK version only when it changes the answer and the target release cannot be inferred.
Instructions
Establish the baseline
Read the applicable repository instructions, nearby code, and target module's build settings. Check the compiler release or, where set separately, the source and target levels. Use those settings with the module's toolchain and runtime requirements to select APIs and language features. Keep the existing release and preview settings. Check unfamiliar APIs against documentation for that release. Preserve established API, serialization, framework, and nullability contracts.
Establish evidence for behavior claims
- For behavior changes and semantic comments, distinguish intended behavior from current behavior. Derive expectations from the user's requirement or an established contract; inspect implementation paths and observed checks to establish current behavior.
- Support non-obvious claims, such as no side effects, idempotency, nullability, or thread safety, with an inspected source location, relevant check, or version-specific contract. Trace applicable normal, no-op, and failure paths through helpers, including state changes before a return or throw. Treat names and existing comments as leads to verify.
- When evidence is missing, narrow the claim or mark it unverified. Resolve uncertainties that affect correctness before making a change that depends on them; continue independent work.
Apply the rules that match the change
For changes affecting behavior, signatures, or result use, trace relevant callers and check how return values affect application behavior. Retain return information required by that behavior or an established contract; otherwise simplify within the task scope. Verify compatibility from actual consumers and integration requirements, not visibility alone. If external consumers or reflective/framework entry points cannot be checked, preserve the existing contract and report the compatibility limit; an empty local search does not establish that a contract is unused.
Read every reference section that applies, including when one expression involves several contracts (for example, decimal equality in a map key).
- For decimal precision and equality, boxed values, bean names, enum metadata, null, or
Optional, read Values and API contracts. - For collection construction, mutability, null elements, iteration, arrays, views, or lookup, read Collections.
- For string accumulation or delimited fields, read Text.
- For executors, virtual threads, thread-local context, or parallel streams, read Concurrency.
- For switches, text blocks, records, patterns, or sealed types, read Language features.
- For failure signaling, exception handling, closing, unlocking, interruption, or logging, read Resources and failures.
Structure and document Java code
- Use early returns or continues for exceptional cases. Split independent business steps into action-named methods. Name intermediate values, business thresholds, and sentinel values.
- Inspect any user-named reference class for comment language, terminology, and level of detail. Follow explicit user and repository rules; otherwise apply the comment forms below.
- Use
/** ... */Javadoc for class, field, and method declaration comments, including non-public members. Describe class responsibilities, field semantics, and method contracts; include threading requirements, idempotency, and boundary behavior where relevant. - Put implementation-specific rationale beside the relevant statements inside the method,
using
//or/* ... */. Preserve caller-visible constraints when shortening comments. - Explain business rules, units, and exceptional boundaries in the project's comment language with English half-width punctuation and the project's sentence-ending convention. Explain non-obvious field units, special values, collection keys/values, and array indexes as needed. Document units, nullability, and boundaries of public and protected APIs in Javadoc. Reuse inherited documentation and omit redundant descriptions of obvious accessors.
- Add
@authorand@sincewhen the project's or user-designated reference's convention calls for them. Verify values for the target class or change; matching a reference's style does not establish its author or release as the values for this change. Ask only when a required value cannot be established; never invent an author or release. - Use explicit imports, conventional modifier order, braces around control-flow bodies, and
uppercase
Lfor long literals. Group overloads and constructors, use@Overridewhere legal, qualify static members with their class, and keep type-use annotations with their type. - Reserve
UPPER_SNAKE_CASEfor deeply immutablestatic finalconstants. UselowerCamelCasefor loggers and mutablestatic finalobjects.
Format changed handwritten Java files
- Select only changed handwritten Java files. Leave generated sources to their generator.
- Use the repository's configured
google-java-formattask or pinned formatter within that scope. If no formatting task is configured, check build scripts and local tooling for an existinggoogle-java-formatCLI. Verify the actual executable with--versionand--help. - Let the formatter handle wrapping, two-space indentation, K&R braces, and the Google Style 100-column limit with its documented exceptions. Use its check mode when available. Otherwise, compare its stdout with each selected file. Inspect the diff for unrelated edits.
Verify and report
- For behavior changes, choose a targeted check that distinguishes the required outcome from a plausible wrong one. Check state changes and failure effects when relevant, as well as return values. Use the independently established expectation above; copying the implementation into an assertion does not validate its behavior. Run the relevant compilation, tests, and static checks.
- Check new, edited, and affected existing comments against the inspected behavior and the project's form, placement, language, and punctuation rules. For comment-only or formatting-only edits, limit verification to those changes and required project checks; no full test workflow is needed.
- Finish by accounting for each affected contract and semantic claim: it has supporting evidence and an observed relevant check, or an explicit unverified boundary. For comments, source inspection can be that check. Claim a runtime behavior was verified only when an executed check covered it; formatting and compilation alone do not establish behavior.
- Report the change, decisive evidence, actual check results, and remaining limits, including unavailable formatting or tests. Keep this concise; no separate evidence report is required.
- For review findings, give the location, failure condition, impact, and repair. Distinguish correctness defects from style violations. Report in the user's language.
Skill maintenance
When changing this skill's behavior, use the isolated cases in evals/README.md. Keep evaluation prompts and grading criteria out of ordinary Java task context.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

