Files

38 lines
1.1 KiB
HTML

<html>
<body>
Reports inner classes that can be made <code>static</code>.
<p>
On Java 17 and older non-static inner classes contain an implicit reference to their enclosing instance.
Static nested classes do not have such a reference.
Therefore, making an inner class <code>static</code> prevents a common cause of memory leaks
and uses slightly less memory per instance of the class.
<p>
The implicit reference was <a href="https://www.oracle.com/java/technologies/javase/18-relnote-issues.html#JDK-8271623">removed</a>
for non-Serializable inner classes in Java 18.
<p><b>Example:</b></p>
<pre><code>
<b>public class</b> Outer {
<b>class</b> Inner { // not static
<b>public void</b> foo() {
bar("x");
}
<b>private void</b> bar(String string) {}
}
}
</code></pre>
<p>After the quick-fix is applied:</p>
<pre><code>
<b>public class</b> Outer {
<b>static class</b> Inner {
<b>public void</b> foo() {
bar("x");
}
<b>private void</b> bar(String string) {}
}
}
</code></pre>
<!-- tooltip end -->
</body>
</html>