mirror of
https://gitflic.ru/project/openide/openide.git
synced 2025-12-16 14:23:28 +07:00
39 lines
1.2 KiB
HTML
39 lines
1.2 KiB
HTML
<html>
|
|
<body>
|
|
Reports anonymous classes that may be safely replaced with <code>static</code> inner classes.
|
|
An anonymous class may be a <code>static</code> inner class if it doesn't explicitly reference its enclosing instance or local classes from its surrounding method.
|
|
<p>
|
|
A <code>static</code> inner class does not keep an implicit reference to its enclosing instance.
|
|
This prevents a common cause of memory leaks and uses less memory per class instance.
|
|
</p>
|
|
<p>
|
|
Since Java 18, only serializable anonymous classes keep an implicit reference to their enclosing instance,
|
|
if this reference is not used. Therefore, when the module language level is Java 18 or higher,
|
|
this inspection reports serializable classes only.
|
|
</p>
|
|
<p>The quick-fix extracts the anonymous class into a named <code>static</code> inner class.</p>
|
|
<p><b>Example:</b></p>
|
|
<pre><code>
|
|
void sample() {
|
|
Thread thread = new Thread(new Runnable() {
|
|
@Override
|
|
public void run() {
|
|
}
|
|
});
|
|
}
|
|
</code></pre>
|
|
<p>After the quick-fix is applied:</p>
|
|
<pre><code>
|
|
void sample() {
|
|
Thread thread = new Thread(new Task());
|
|
}
|
|
|
|
private static class Task implements Runnable {
|
|
@Override
|
|
public void run() {
|
|
}
|
|
}
|
|
</code></pre>
|
|
<!-- tooltip end -->
|
|
</body>
|
|
</html> |