Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In Java, class variables are initialised in the <a href="http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.3.2" rel="nofollow noreferrer">following order</a>:</p> <ol> <li>Static variables of your superclasses</li> <li>All static variables of this class are set to their <a href="http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.12.5" rel="nofollow noreferrer">default values</a>.</li> <li>Static variables, and static initialisation blocks, in declaration order.</li> <li>Instance variables of your superclasses</li> <li>All instance variables of this class are set to their <a href="http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.12.5" rel="nofollow noreferrer">default values</a>.</li> <li>Instance variables, and instance level initialisation blocks, in declaration order</li> </ol> <p>1 &amp; 2 are only done the very first time that a class is instantiated.</p> <p>So, given the following code:</p> <pre><code>class Test extends TestSuper { final int ti1; final int ti2 = counter ++; { ti1 = counter ++; } static final int ts1; static final int ts2 = counter ++; static { ts1 = counter ++; } public static void main(String[] argv) { Test test1 = new Test(); printTest(test1); Test test2 = new Test(); printTest(test2); } private static void printTest(Test test) { System.out.print("ss2 = " + test.ss2); System.out.print(", ss1 = " + test.ss1); System.out.print(", ts2 = " + test.ts2); System.out.println(", ts1 = " + test.ts1); System.out.print("si2 = " + test.si2); System.out.print(", si1 = " + test.si1); System.out.print(", ti2 = " + test.ti2); System.out.println(", ti1 = " + test.ti1); System.out.println("counter = " + test.counter); } } class TestSuper { static int counter = 0; final int si1; final int si2 = counter ++; { si1 = counter ++; } static final int ss1; static final int ss2 = counter ++; static { ss1 = counter ++; } } </code></pre> <p>Then we get the following output:</p> <pre><code>ss2 = 0, ss1 = 1, ts2 = 2, ts1 = 3 si2 = 4, si1 = 5, ti2 = 6, ti1 = 7 counter = 8 ss2 = 0, ss1 = 1, ts2 = 2, ts1 = 3 si2 = 8, si1 = 9, ti2 = 10, ti1 = 11 counter = 12 </code></pre> <p>From this output we can see that the fields are initialised in the order specified in the list.</p> <p>Now, as to the second question, can re-ordering the fields change the class behaviour. Yes, by re-ordering the fields you change the initialisation order of the fields. Now, in the specific case where all of the fields are independent, this won't affect the observed behaviour, however whenever the fields are not independent, for example in the above code, then re-ordering the fields could change their initialised values.</p> <p>For example, if the three lines:</p> <pre><code> static final int ss1; static final int ss2 = counter ++; static { ss1 = counter ++; } </code></pre> <p>are changed to:</p> <pre><code> static final int ss1; static { ss1 = counter ++; } static final int ss2 = counter ++; </code></pre> <p>Then the output would change to:</p> <pre><code>ss2 = 1, ss1 = 0, ts2 = 2, ts1 = 3 si2 = 4, si1 = 5, ti2 = 6, ti1 = 7 counter = 8 </code></pre> <p>That is, <code>ss2</code>, and <code>ss1</code> would change values.</p> <p>The reason for this is that this behaviour is specified in the <a href="http://java.sun.com/docs/books/jls/index.html" rel="nofollow noreferrer">Java Language Specification</a>.</p>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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