import java.io.*; import java.util.*; /** * *@version 2009/6/5 *@author Rorely */ public class ConstructorTest{ public static void main(String[] args){ Employee[] staff=new Employee[3]; staff[0]=new Employee("Harry",345678); staff[1]=new Employee(456342); staff[2]=new Employee(); for(Employee e:staff) System.out.println("name="+e.getName()+"; id="+e.getId()+"; salary="+e.getSalary()); } } class Employee{ //variables private String name="";//instance field initialization private static int nextId; private int id; private double salary; //static initialization block static{ Random generator=new Random(); nextId=generator.nextInt(10000); } //object initialization block { id=nextId; nextId++; } //constructor public Employee(){ //name initialized to "" //salary not explicitly set--initialized to 0 //id initialized in initialization block } public Employee(String name,double salary){ this.name=name; this.salary=salary; } public Employee(double salary){ this("Employee #"+nextId,salary); } //methods public String getName(){ return name; } public double getSalary(){ return salary; } public int getId(){ return id; } public void setName(String name){ this.name= name; } public void setSalary(double salary){ this.salary= salary; } public void setId(int id){ this.id=id; } }