Files
openide/java/java-impl/resources/inspectionDescriptions/NonThreadSafeLazyInitialization.html
Leonid Shalupov 40795fe787 IJI-2422: community/java: move resources under resources root
GitOrigin-RevId: 8b2b63fc6db476ca0c2cfe5cadd84db6c4236d0f
2025-02-05 04:43:28 +00:00

36 lines
1.2 KiB
HTML

<html>
<body>
Reports <code>static</code> variables that are lazily initialized in a non-thread-safe manner.
<p>Lazy initialization of <code>static</code> variables should be done with an appropriate synchronization construct
to prevent different threads from performing conflicting initialization.</p>
<p>When applicable, a quick-fix, which introduces the
<a href="https://en.wikipedia.org/wiki/Initialization_on_demand_holder_idiom">lazy initialization holder class idiom</a>, is suggested.
This idiom makes use of the fact that the JVM guarantees that a class will not be initialized until it is used.
<p><b>Example:</b></p>
<pre><code>
class X {
private static List&lt;String&gt; list;
public List&lt;String&gt; getList() {
if (list == null) {
list = List.of("one", "two", "tree");
}
return list;
}
}
</code></pre>
<p>After the quick-fix is applied:</p>
<pre><code>
class X {
private static final class ListHolder {
static final List&lt;String&gt; list = List.of("one", "two", "tree");
}
public List&lt;String&gt; getList() {
return ListHolder.list;
}
}
</code></pre>
<!-- tooltip end -->
</body>
</html>