Private Method Could Be Static
ID |
java.method_could_be_static |
Severity |
info |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Efficiency |
Language |
Java |
Tags |
best-practice, efficiency |
Description
Reports private instance methods that do not access any instance state and could be declared static.
Rationale
A method that never reads or writes instance fields, never calls instance methods, and never uses this or super carries no dependency on the object’s state. Declaring such a method static communicates this independence clearly, avoids passing an unnecessary receiver, and enables minor JVM optimizations.
The rule is conservative: only private methods are flagged to avoid breaking API contracts for non-private methods. Methods annotated @Override, serialization hooks (readObject, writeObject, readResolve, writeReplace), and methods in non-static inner classes are excluded.
// Bad -- method does not use instance state
public class Formatter {
private String format(int value) {
return "value=" + value;
}
}
Remediation
Add the static modifier to the method. Callers inside the same class do not need to change.
// Good -- declared static
public class Formatter {
private static String format(int value) {
return "value=" + value;
}
}