java-minimize-static-methods

A Java coding rule that prefers ordinary instance methods in business, service, and component classes instead of static methods. Static methods remain suitable for separate, stateless utility classes.

In plain words
What is it for?
Use it when designing or reviewing Java services, components, helper methods, and utility classes.
Why use it?
Instance methods work more naturally with injected collaborators and are easier to replace or test. The rule also prevents business logic from becoming a collection of static helpers.

Cursor rule for Cursor

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/xc404/kiwi/java-minimize-static-methods
Clone the repo
git clone --depth 1 https://github.com/xc404/kiwi

Made for: Cursor.

Per session 588 This file is loaded in full into every session.
When invoked 588 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.00588 $0.00588
Opus 5 $0.00294 $0.00294
Sonnet 5 $0.00118 $0.00118
Haiku 4.5 $0.00059 $0.00059

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

Security

Grade A, and why

java-minimize-static-methods 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.

.cursor/rules/java-minimize-static-methods.mdc · 51 lines

What it actually says

尽量少实现静态方法(Java)

在本仓库的 Java 代码中,优先使用实例方法;通过构造注入或 Spring 注入协作对象,便于测试与扩展。避免在普通业务类、服务类、组件类中堆砌 static 方法。

例外:工具类

以下情形可使用 仅含静态 API 的风格(常见命名如 *Utils*Helper,或 final 类 + private 构造器):

  • 纯函数式、无状态的通用转换、格式化、校验、集合/字符串小工具;
  • 明确设计为 library-style 的可复用静态入口,且不携带 Spring 生命周期或可变全局状态。

不推荐

public class OrderService {
    public static void validate(Order o) { ... }
    public static BigDecimal calcTax(Order o) { ... }
}

推荐

@Component
public class OrderService {
    public void validate(Order o) { ... }
    public BigDecimal calcTax(Order o) { ... }
}

或把纯算法抽到 OrderTaxUtils命名清晰的工具类,而不是在领域服务上挂 static

private static 与普通类里的静态方法

业务类、服务类、组件类 等「常规类」中:

  • 不要为了整理结构而写 private static 辅助方法:与公开静态 API 一样,优先改成 private 实例方法(不依赖注入时同样是实例方法,便于与类内其它逻辑一致、子类可覆盖(若需要)、测试时也可替换)。
  • 没有必要时,生成代码阶段就不要引入静态方法;若逻辑是纯函数且会在多处复用,再 抽到专用的 *Utils / *Helper,而不是留在当前类里用 static

简言之:要么实例方法,要么独立工具类;常规类内 static 方法(含 private static)默认不写

static final 常量

本规则针对 方法static final 常量命名仍遵循仓库内 Java 常量约定(见 java-enum-static-constants-naming)。

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 · 51 lines · 588 tokens per session scan A 048f852cb564

Subscribe to this mod's changes

java-minimize-static-methods is a cursor rule published in the GitHub repository xc404/kiwi (7 stars, last pushed 7d ago), licensed MIT. It adds 588 tokens to every session, about $0.0029 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-31.