This inspection is especially useful for Pascal style declarations at the beginning of a method. Additionally variables with too broad a scope are also often left behind after refactorings.
Example:
StringBuilder sb = new StringBuilder();
System.out.println();
sb.append(1);
After the quick-fix is applied:
System.out.println();
StringBuilder sb = new StringBuilder();
sb.append(1);
Configure the inspection:
sb
variable above.
However, it will be suggested for the following code:
StringBuilder sb = new StringBuilder(a);
if (flag) {
sb.append(1);
}
foo
variable:
class Foo {
static List<Foo> fooList = new ArrayList<>();
String bar;
Foo(String bar) {
this.bar = bar;
fooList.add(this);
}
public static void main(String[] args) {
// movement is possible even though is unsafe
Foo foo = new Foo("bar");
System.out.println(fooList.size());
System.out.println(foo.bar);
}
}