next up previous contents
Next: Exceptions Up: Java Notes Previous: Inner and Nested Classes:

Inner and Nested Classes: Anonymous 5

public class InitializerDemo{
  public int[] array1; // instance variable
  
  {// instance initializer; 
   // runs for every new instance after the 
   // sueprclass constructors and 
   // before the class constructors, if any
    array1 = new int[10];
    for(i=0;i<10;i++){
      array[i]=1;
    }
  }

  // another instance initializer; 
  // runs in the order it appears in the class
  int[] array2 = new int[10];
  {
    for(i=0;i<10;i++){
      array2[i]=2*i;
  }

  //static initialiser; 
  //runs only once when class first loaded
  static int[] static_array = new int[10];
  static{
    for(int i=0; i<10;i++){
      static_array=i;
    }
  }
}
/******** sample compilation & run *******
# javac l1Anon5.java 
# java l1Anon5      
In class l1Anon5: void main(String argv[]) is not defined

#
******************************************/



Ananda Amatya
9/15/1999