Unnecessary Default Constructor
ID |
java.unnecessary_default_constructor |
Severity |
low |
Remediation Complexity |
medium |
Remediation Risk |
low |
Remediation Effort |
medium |
Resource |
Code Smell |
Language |
Java |
Tags |
code-style |
Description
Reports classes with a single explicit no-argument constructor whose body is empty. Java automatically provides a default no-arg constructor when no constructors are declared, so the explicit one is redundant.
Rationale
An empty public no-arg constructor adds boilerplate without changing behavior. Removing it reduces noise and makes the code cleaner. If the intent is to document the constructor or to have a specific access modifier, a comment or a non-empty body makes the purpose clear.
// Bad -- redundant constructor
public class MyService {
public MyService() {
}
public void process() { }
}
// Good -- no explicit constructor needed
public class MyService {
public void process() { }
}
Remediation
Remove the empty no-arg constructor. If a specific access modifier is needed (e.g., private for utility classes), keep it but that is a different intent.