Unused Private Method
ID |
java.unused_private_method |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Code Smell |
Language |
Java |
Tags |
CWE:561, code-style, unused-code |
Description
Reports private methods that are never called within the class body.
A method a framework invokes is not reported: JavaFX @FXML handlers wired by name from the FXML
markup, JUnit lifecycle and @MethodSource methods, Spring @EventListener, @Scheduled,
@PostConstruct and @PreDestroy. The call is in a resource file or in the container, so the
absence of a caller in this file says nothing.
Rationale
A private method that is never invoked is dead code. Unlike public or protected methods, a private method cannot be called from outside the class, so if it is not referenced internally it is guaranteed to be unreachable. Dead methods increase maintenance cost and obscure the class’s actual behavior.
// Bad -- method is never called
public class Processor {
private void legacyProcess() {
// old code
}
public void run() {
System.out.println("running");
}
}
// Good -- remove the dead method
public class Processor {
public void run() {
System.out.println("running");
}
}
Remediation
Remove the unused private method. If it is needed for future use, reintroduce it when actually called.