title: How are object dependencies between static blocks resolved?
body: <p>I recently came across this at work. While I am not sure it is really a good idea, I don't understand how static blocks are handled by the compiler.</p>
  
  <p>Here is an example:</p>
  
  <p>Consider that you have classes <code>A</code> and <code>B</code>:</p>
  
  <pre><code>public class A {
  
      public final static List&lt;Integer&gt; list;
      static {
          list = new ArrayList&lt;&gt;();
      }
  }
  
  public class B {
  
      public final static int dependsOnA;
      static {
          dependsOnA = A.list.size();
      }
  }
  </code></pre>
  
  <p>And a main class that just reads <code>B.dependsOnA</code>.</p>
  
  <p>The static block in <code>B</code> is dependent of the one in <code>A</code>, since it uses the <code>list</code> static variable.</p>
  
  <p>Now, the code executes properly and no <code>NullPointerException</code> is raised at runtime. But what is the mechanism that ensures that <code>list</code> is initialized before it is potentially used elsewhere?</p>