/**
   @version 1.01 2004-02-19
   @author Cay Horstmann
*/import java.util.*;public class ConstructorTest
{
   public static void main(String[] args)
   {
      // fill the staff array with three Employee objects
      Employee[] staff = new Employee[3];      staff[0] = new Employee("Harry", 40000);
      staff[1] = new Employee(60000);
      staff[2] = new Employee();      // print out information about all Employee objects
      for (Employee e : staff)
         System.out.println("name=" + e.getName()
            + ",id=" + e.getId()
            + ",salary=" + e.getSalary());
   }
}class Employee
{
   // three overloaded constructors
   public Employee(String n, double s)
   {
      name = n;
      salary = s;
   }   public Employee(double s)
   {
      // calls the Employee(String, double) constructor
      this("Employee #" + nextId, s);
   }   // the default constructor
   public Employee()
   {
      // name initialized to ""--see below
      // salary not explicitly set--initialized to 0
      // id initialized in initialization block
   }   public String getName()
   {
      return name;
   }   public double getSalary()
   {
      return salary;
   }   public int getId()
   {
      return id;
   }   private static int nextId;   private int id;
   private String name = ""; // instance field initialization
   private double salary;   // static initialization block
   static
   {
      Random generator = new Random();
      // set nextId to a random number between 0 and 9999
      nextId = generator.nextInt(10000);
   }   // object initialization block 什么是对象初始化块?什么时候被初始化?
   {
      id = nextId;
      nextId++;
   }
}

解决方案 »

  1.   

    static 
       { 
          Random generator = new Random(); 
          // set nextId to a random number between 0 and 9999 
          nextId = generator.nextInt(10000); 
       } 
    上面这段是类被加载时调用   // object initialization block 什么是对象初始化块?什么时候被初始化? 
       { 
          id = nextId; 
          nextId++; 
       } 
    实例化该类的对象时调用
      

  2.   

    静态初始化块:使用关键字static定义的代码块。当类装载到系统时执行一次,静态初始化块只能初始化类的静态数据成员。非静态初始化块:对每个要生成的对象执行一次。可以初始化静态数据成员以及实例数据成员。一个类可以有多个初始化块,在同种类型情况下,它们将按照在程序中出现的顺序运行,但是有如下规则:静态初始化块在非静态初始化块之前执行。静态/非静态初始化块在构造函数之前执行。当类创建一个对象了的初始顺序:
    1.  父类静态成员和静态初始化块 ,按在代码中出现的顺序依次执行 
    2.  子类静态成员和静态初始化块 ,按在代码中出现的顺序依次执行 
    3.  父类实例成员和实例初始化块 ,按在代码中出现的顺序依次执行 
    4.  父类构造方法 
    5.  子类实例成员和实例初始化块 ,按在代码中出现的顺序依次执行 
    6.  子类构造方法 
      

  3.   

    /** 
       @version 1.01 2004-02-19 
       @author Cay Horstmann 
    */ import java.util.*; public class ConstructorTest 

       public static void main(String[] args) 
       { 
          // fill the staff array with three Employee objects 
          Employee[] staff = new Employee[3];       staff[0] = new Employee("Harry", 40000); 
          staff[1] = new Employee(60000); 
          staff[2] = new Employee();       // print out information about all Employee objects 
          for (Employee e : staff) 
             System.out.println("name=" + e.getName() 
                + ",id=" + e.getId() 
                + ",salary=" + e.getSalary()); 
       } 
    } class Employee 

       // three overloaded constructors 
       public Employee(String n, double s) 
       { 
          name = n; 
          salary = s; 
       }    public Employee(double s) 
       { 
          // calls the Employee(String, double) constructor 
          this("Employee #" + nextId, s); 
       }    // the default constructor 
       public Employee() 
       { 
          // name initialized to ""--see below 
          // salary not explicitly set--initialized to 0 
          // id initialized in initialization block 
       }    public String getName() 
       { 
          return name; 
       }    public double getSalary() 
       { 
          return salary; 
       }    public int getId() 
       { 
          return id; 
       }    private static int nextId;    private int id; 
       private String name = ""; // instance field initialization 
       private double salary;    // static initialization block 
       static 
       { 
          Random generator = new Random(); 
          // set nextId to a random number between 0 and 9999 
          nextId = generator.nextInt(10000); 
       }    // object initialization block 什么是对象初始化块?什么时候被初始化? 
       { 
          id = nextId; 
          nextId++; 
       } 

      

  4.   

    想知道到那段代码在什么时候被调用很简单,你只要在那段代码前加上System.out.println("xxx方法被调用");就行了!不用死记硬背。