Parameter Shadows Field

ID

java.parameter_shadows_field

Severity

low

Remediation Complexity

medium

Remediation Risk

low

Remediation Effort

medium

Resource

Code Smell

Language

Java

Tags

code-style, naming

Description

Reports method parameters whose name matches a field declared in the same class.

Rationale

When a parameter name shadows a field, the unqualified name inside the method body refers to the parameter, not the field. This is a common source of initialization bugs: the developer writes name = name (a no-op on the field) instead of this.name = name.

By default, constructors and setter methods are excluded because this.field = field is the standard Java/Spring DI convention in those contexts.

// Flagged -- non-setter method parameter shadows field
private String label;

public void update(String label) {
    process(label); // is this the parameter or was this.label intended?
}

Remediation

Use a different name for the parameter:

// Good -- clear distinction
private String name;

public void setName(String newName) {
    this.name = newName;
}