All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sonar.l10n.java.rules.java.S1117.html Maven / Gradle / Ivy

The newest version!

Why is this an issue?

Shadowing occurs when a local variable has the same name as a variable or a field in an outer scope.

This can lead to three main problems:

  • Confusion: The same name can refer to different variables in different parts of the scope, making the code hard to read and understand.
  • Unintended Behavior: You might accidentally use the wrong variable, leading to hard-to-detect bugs.
  • Maintenance Issues: If the inner variable is removed or renamed, the code’s behavior might change unexpectedly because the outer variable is now being used.

To avoid these problems, rename the shadowing, shadowed, or both identifiers to accurately represent their purpose with unique and meaningful names.

This rule focuses on variables in methods that shadow a field.

Noncompliant code example

class Foo {
  public int myField;

  public void doSomething() {
    int myField = 0; // Noncompliant
    // ...
  }
}

Resources

Documentation

Related rules

  • {rule:java:S2176} - Class names should not shadow interfaces or superclasses
  • {rule:java:S2387} - Child class fields should not shadow parent class fields
  • {rule:java:S4977} - Type parameters should not shadow other type parameters




© 2015 - 2025 Weber Informatics LLC | Privacy Policy