Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>It's a <code>static</code> initializer. Analogously to the instance initializer (<a href="http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.6" rel="nofollow noreferrer">§8.6</a>), you can use it to initialize your <code>class</code> when it's loaded. It is <em>NOT</em> "invoked" explicitly; it is executed automatically when the <code>class</code> is loaded, in textual order (<code>static</code> initializer that occurs later in the text is guaranteed to be performed later during the initialization).</p> <p>You can use a <code>static</code> initializer to:</p> <ul> <li>Initialize some <code>static</code> fields</li> <li>Perform some other one-time calculations, perhaps something requiring a <code>try-catch</code> block, logging events related to the loading of the <code>class</code>, making sure that Java's assertion is enabled, etc.</li> </ul> <p>There are some caveats, e.g. a <code>class</code> may be reloaded, and usually there are alternatives of writing it (e.g. refactoring into a <code>private static</code> named method), but using a <code>static</code> initializer is an option.</p> <h3>References</h3> <ul> <li><a href="http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.7" rel="nofollow noreferrer">JLS 8.7 Static initializers</a></li> <li><a href="http://java.sun.com/docs/books/jls/third_edition/html/execution.html#12.4" rel="nofollow noreferrer">JLS 12.4 Initialization of Classes and Interfaces</a></li> </ul> <h3>Related questions</h3> <p>These questions discuss various aspects of <code>static</code> and instance initializers usage:</p> <ul> <li><a href="https://stackoverflow.com/questions/335311/static-initializer-in-java">Static initializer in Java</a></li> <li><a href="https://stackoverflow.com/questions/1866770/how-to-handle-a-static-final-field-initializer-that-throws-checked-exception">How to handle a static final field initializer that throws checked exception</a></li> <li><a href="https://stackoverflow.com/questions/1187448/why-is-the-order-of-declarations-important-for-static-initializers">Why is the order of declarations important for static initializers?</a></li> <li><a href="https://stackoverflow.com/questions/804589/use-of-initializers-vs-constructors-in-java">Use of Initializers vs Constructors in Java</a></li> <li><a href="https://stackoverflow.com/questions/878577/are-java-static-initializers-thread-safe">Are Java static initializers thread safe?</a></li> </ul> <hr> <h3>An example usage: requiring that assertions are enabled</h3> <p>The following is the prescribed idiom in the <a href="http://download.oracle.com/javase/1.4.2/docs/guide/lang/assert.html" rel="nofollow noreferrer">Java Language Guide for Programming With Assertions</a> to ensure that assertion is enabled at run-time:</p> <blockquote> <h3>Requiring that Assertions are Enabled</h3> <p>Programmers of certain critical systems might wish to ensure that assertions are not disabled in the field. The following static initialization idiom prevents a class from being initialized if its assertions have been disabled:</p> <pre><code>static { boolean assertsEnabled = false; assert assertsEnabled = true; // Intentional side effect!!! if (!assertsEnabled) throw new RuntimeException("Asserts must be enabled!!!"); } </code></pre> <p>Put this <code>static</code>-initializer at the top of your class.</p> </blockquote> <p>By putting this snippet in a <code>static</code> initializer for the class, the code will be one of the first thing executed when the class is loaded, before any instances of the class can be created. The code itself checks if Java assertion is enabled, and throws a <code>RuntimeException</code> if it's not.</p> <h3>See also</h3> <ul> <li><a href="http://en.wikipedia.org/wiki/Assertion_%28computing%29" rel="nofollow noreferrer">Wikipedia/Assertion (computing)</a></li> </ul> <h3>Related questions</h3> <ul> <li><a href="https://stackoverflow.com/questions/1208404/do-you-use-assertions">Do you use assertions?</a></li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload