Back to Intellij Community

PatternVariableCanBeUsed

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

2025.3-rc-2341 B
Original Source

Reports local variable declarations that can be replaced with pattern variables, which are usually more compact.

Example:

if (obj instanceof String) {
    String str = (String) obj;
    System.out.println(str);
  }

Can be replaced with:

if (obj instanceof String str) {
    System.out.println(str);
  }

New in 2020.1