Back to Intellij Community

FieldCanBeLocal

java/java-impl/resources/inspectionDescriptions/FieldCanBeLocal.html

2025.3-rc-2507 B
Original Source

Reports private fields that are redundant and can be replaced with a local variable.

If all local usages of a field are preceded by an assignment to that field, the field can be replaced with one or more local variables.

Example:

class DeskLamp {
    private boolean on = false;

    boolean pressButton() {
      on = true;
      return on;
    }
  }

After the quick-fix is applied:

class DeskLamp {
    boolean pressButton() {
      boolean on = true;
      return on;
    }
  }