title: Understanding static variable initialization in Java
body: <pre><code>public class InstanceBuilder {
  
      private static final InstanceBuilder INSTANCE = new InstanceBuilder();
  
      private static String name = null;
  
      private InstanceBuilder() {
          System.out.println("Setting cons()");
          name = "Testing";
      }
  
      public static String getName() {
          return name;
      }
  }
  
  public class Driver {
  
      public static void main(String[] args) {
          // TODO Auto-generated method stub
          System.out.println("value is " + InstanceBuilder.getName());
      }
  
  }
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>Setting cons()
  value is null
  </code></pre>
  
  <p>Why does it print value as <code>null</code> even though I have set the <code>static</code> variable in constructor and it is called as expected. If I try to print in constructor, it is printing <code>Testing</code>, but if I call from <code>public static</code> method, it is <code>null</code>. I know if I change it to <code>INSTANCE.name</code>, it works.
  But I want to understand why it is not working if I directly access <code>static</code> variable since same copy should be shared.
  What I am missing here? </p>