next up previous contents
Next: Classes: this Up: Java Notes Previous: Classes: Static Methods 1

Classes: Static Initializer 1

/**
 *  This code is from the book:
 *  Winder, R and Roberts, G (1998)
 *  Developing Java Software
 *  John Wiley & Sons.
 *  It is copyright (c) 1997 Russel Winder
 *  and Graham Roberts.
 */

class l1ClStat3{//Constructor2
  //The single private constructor prevents
  //any client of the class creating
  //instance objects directly using new.
  private l1ClStat3(String s){
    name = s;
  }

  public String toString(){
    return "l1ClStat3 object: " + name;
  }

  public static l1ClStat3 getInstance(){
    if (next >= SIZE){
      next = 0;
    }
    return objects[next++];
  }

  private String name;

  private static final int SIZE = 10;
  private static int next = 0;

  // The next variable has an initializer block
  // This is not a method!
  private static l1ClStat3[] objects = 
			new l1ClStat3[SIZE];
  static{
    for (int i = 0; i < SIZE; i++){
      objects[i] = new l1ClStat3(
		      new Integer(i).toString());
      System.out.println("here");
    }
  }
}
/******** sample compilation & run ********
# javac l1ClStat3.java 
# java l1ClStat3
here
here
here
here
here
here
here
here
here
here
In class l1ClStat3: void main(String argv[]) is not defined
******************************************/



Ananda Amatya
9/15/1999