The Java Program: Order.java

  1 // Order.java -- illustrate static initialization and order dependencies
  2 
  3 public class Order {
  4   static int a = 1;
  5   static {
  6     a++;
  7 //  a = b;
  8 //  b = 7;
  9  }
 10   static Later L = new Later();
 11   static int b = 2;
 12   static int d = Later.c;
 13 
 14   // initialized instance variables
 15 //int i1 = j1+2;   // Illegal forward reference to instance variable
 16   int j1 = 4;
 17 
 18   // initialized static variables
 19 //static int i2 = j2+2;  // Illegal forward reference to static varable
 20   static int j2 = 4;
 21 
 22   public static void main (String args[]) {
 23     System.out.println ("a = " + a);   // a=3
 24     System.out.println ("b = " + b);   // b=3
 25     System.out.println ("d = " + d);   // d=0
 26   }
 27 
 28   static int x = sm();
 29   static int k = 78;
 30   static int sm () {return (k);};
 31 
 32   int l = 93;
 33   int im () {return (l);};
 34 
 35   static {
 36     a++;
 37     b = 3;
 38   }
 39 }
 40 
 41 
 42 class Later {
 43   static int c = Order.d;
 44 }
 45 
 46 // NB. Constructors can't be static (or native, abstract,
 47 // synchronized or final