Inspectopedia Help

Local variable hides field

Reports local variables named identically to a field of a surrounding class. As a result of such naming, you may accidentally use the variable where the identically named field is intended.

A quick-fix is suggested to rename the variable.

Example:

public class Foo { public Object foo; void bar() { Object o = new Object() { void baz() { Object foo; // Local variable 'foo' hides field in class 'Foo' } }; } }

You can configure the following options for this inspection:

  • Ignore non-accessible fields - ignore local variables named identically to superclass fields that are not visible (for example, because they are private).

  • Ignore local variables in a static context hiding non-static fields - for example when the local variable is inside a static method or inside a method which is inside a static inner class.

Inspection options

Option

Type

Default

Ignore non-accessible fields

Checkbox

true

Ignore local variables in a static context hiding non-static fields

Checkbox

true

Inspection Details

Available in:

IntelliJ IDEA 2023.3, Qodana for JVM 2023.3

Plugin:

Java, 233.SNAPSHOT

Last modified: 13 July 2023