title: Internal working of static and non-static forward references in Java
body: <p>I am working with forward references in Java and wondering why Java allows forward reference with either <code>ClassName</code> (in static variable) or with <code>this</code> reference in case of instance variable? What is the <strong>background process</strong> that is taking place <strong>at the level of JVM?</strong> For instance:</p>
  
  <p><strong>Static Forward Reference-</strong></p>
  
  <pre><code>class StaticForwardReferences {
      static {
          sf1 = 10;   // (1)
          int b = sf1 = 20;   // (2)
          int c = StaticForwardReferences.sf1; // (3) Works fine
          // Above statement allows the allocation of value of 'sf1'
          // to variable 'c' just because it is accessed with class name
          // instead of direct name
  
          // whereas below statement throws illegal forward reference
          // error at compile time
          System.out.println(sf1); // (4) Illegal forward reference
      }
      static int sf1 = sf2 = 30;
      static int sf2;
  
      public static void main(String[] args) {}
  }
  </code></pre>
  
  <p>Is there any kind of temporary storage where the values get stored when we do a forward reference by assigning the variables before their declaration as done in (1) and (2) steps above, if we print the value of variable <code>c</code>, it shows the value from the most recent assignment did to <code>sf1</code>.</p>
  
  <p><strong>Non-static Forward Reference-</strong></p>
  
  <pre><code>class NonStaticForwardReferences {
      {
          nsf1 = 10;
          System.out.println(this.nsf1);  // 10
          nsf1 = sf1;
          // System.out.println(nsf1); Illegal forward reference
          int b = nsf1 = 20;
  
          int c = this.nsf1;
          System.out.println(c);  // 20
          // why variable 'c' is initialized to 20 when used with 'this' reference
          // instead of showing illegal forward reference, how it works in the background?
     }
  
     int nsf1 = nsf2 = 30;
     int nsf2;
     static int sf1 = 5;
  
     public static void main(String[] args) {}
  }
  </code></pre>
  
  <p>Please put some light on the <strong>background process</strong> that is taking place behind the scenes for the above two cases. Thanks in advance! :)</p>