Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h1>Local variables</h1> <p>A <code>final</code> <strong>variable</strong> needs to be assigned a value exactly once <strong>before it is accessed.</strong> This means that if it's never assigned a value and never accessed, the compiler won't complain.</p> <pre><code>void foo() { final int a; // Never assigned, but never accessed either, so no problem final int b = 7; System.out.println("b is " + b); // System.out.println("a is " + a); // Uncommenting the above line would cause a compile error } </code></pre> <h1>Static fields</h1> <p>A similar logic applies to <code>final</code> <strong>static fields</strong>, except it's assumed that they will be accessed at some point, so they must be initialized either on the definition line or in a static initializer block. </p> <p>Here's what <a href="http://docs.oracle.com/javase/tutorial/java/javaOO/initial.html" rel="nofollow">the Java tutorial</a> has to say about static initialization blocks:</p> <blockquote> <p>This works well when the initialization value is available and the initialization can be put on one line. However, this form of initialization has limitations because of its simplicity. If initialization requires some logic (for example, error handling or a for loop to fill a complex array), simple assignment is inadequate. Instance variables can be initialized in constructors, where error handling or other logic can be used. To provide the same capability for class variables, the Java programming language includes static initialization blocks.</p> <p>Note: It is not necessary to declare fields at the beginning of the class definition, although this is the most common practice. It is only necessary that they be declared and initialized before they are used.</p> </blockquote> <h1>Instance fields</h1> <p>While we're at it, a <code>final</code> <strong>instance (non-static) field</strong> must be assigned a value exactly once <strong>by the time the instance initialization is complete</strong>. This means there are three places where you can initialize one (but you must pick one):</p> <h3>1. Definition line:</h3> <pre><code>// For when you know the answer class Foo { final int theAnswer = 42; } </code></pre> <h3>2. Constructor:</h3> <pre><code>// For when you need to have the answer passed in class Foo { final int theAnswer; Foo(int answer) { theAnswer = answer; } } // Or for when you need to do some computation class Bar { static final int ANSWER_COUNT = 10; final int[] answers; Foo() { answers = new int[ANSWER_COUNT]; for (int i = 0; i &lt; ANSWER_COUNT; i++) { answers[i] = i; } } </code></pre> <h3>3. Initializer block:</h3> <pre><code>// For when you need to do some computation and have many constructors class Bar { static final int ANSWER_COUNT = 10; final int[] answers; { answers = new int[ANSWER_COUNT]; for (int i = 0; i &lt; ANSWER_COUNT; i++) { answers[i] = i; } } // I have many constructors and don't want to copy-paste // the initialization logic above to all of them Bar() { ... } Bar(int i) { ... } Bar(String blah) { ... } } </code></pre> <p>From <a href="http://docs.oracle.com/javase/tutorial/java/javaOO/initial.html" rel="nofollow">the same page in the tutorial</a>, regarding initializer blocks:</p> <blockquote> <p>The Java compiler copies initializer blocks into every constructor. Therefore, this approach can be used to share a block of code between multiple constructors.</p> </blockquote>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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