Local variables should not shadow class fields

  • squid : HiddenFieldCheck

Shadowing fields with a local variable is a bad practice reducing code readability: It makes it confusing to know whether the field or the variable is and should be accessed.

The following code illustrates this rule:

class Foo {
  public int myField;

  public Foo(int myField) {                   // Compliant - method parameters are not checked
    this.myField = myField;
  }

  @Override
  public String toString() {
    int myField = 0;                          // Non-Compliant - should be renamed
    return "Foo{MyField: " + myField + "}";
  }
}
Close