Files
2025-02-05 04:43:28 +00:00

55 lines
1.9 KiB
HTML

<html>
<body>
Reports any variable declarations that can be moved to a smaller scope.
<p>This inspection is especially
useful for <i>Pascal style</i> declarations at the beginning of a method. Additionally variables with too broad a
scope are also often left behind after refactorings.</p>
<p><b>Example:</b></p>
<pre><code>
StringBuilder sb = new StringBuilder();
System.out.println();
sb.append(1);
</code></pre>
<p>After the quick-fix is applied:</p>
<pre><code>
System.out.println();
StringBuilder sb = new StringBuilder();
sb.append(1);
</code></pre>
<!-- tooltip end -->
<p>Configure the inspection:</p>
<ul>
<li>Use the <b>Only report variables that can be moved into inner blocks</b> option to report only those variables that can be moved inside deeper code blocks.
For example, when the option is enabled, the movement will not be suggested for the <code>sb</code> variable above.
However, it will be suggested for the following code:
<pre><code>
StringBuilder sb = new StringBuilder(a);
if (flag) {
sb.append(1);
}
</code></pre></li>
<li>Use the <b>Report variables with a new expression as initializer
(potentially unsafe)</b> option to report variables that are initialized with a new expression. This makes the inspection potentially
unsafe when the constructor has non-local side effects. For example, when the option is enabled, the movement will be
suggested for the <code>foo</code> variable:
<pre><code>
class Foo {
static List&lt;Foo&gt; fooList = new ArrayList&lt;&gt;();
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);
}
}
</code></pre></li>
</ul>
</body>
</html>