Redundant Field Initializer
ID |
java.redundant_field_initializer |
Severity |
info |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Code Smell |
Language |
Java |
Tags |
best-practice, code-style |
Description
Reports field declarations that are explicitly initialized to the default value for their type. In Java, instance and class fields are automatically initialized to their type’s default value (0 for numeric types, false for boolean, '\0' for char, and null for reference types). Explicitly writing these defaults adds visual noise without changing behavior.
Rationale
Redundant initializers clutter the code and can mislead readers into thinking the value was deliberately chosen when it is simply the language default. Omitting them makes the code more concise and makes truly intentional initializations stand out.
// Bad -- redundant initializers
int count = 0;
boolean ready = false;
String name = null;
Only the char literal written exactly '\0' counts as the default for char. An escape sequence carries a real value, and none of these is reported:
// OK -- not the default char
static final char WINDOWS_SEPARATOR = '\\';
static final char LF = '\n';
Remediation
Remove the redundant initializer and let Java apply the default automatically. If the value is intentional and important for readability, consider adding a comment instead.
// Good -- rely on Java defaults
int count;
boolean ready;
String name;