需要注意,不需要使用对象调用静态方法。例如,不需要构造Math类对象就可以调用Math.pow。

    同理,main方法也是一个静态方法。

 
  
  1. public class Application 
  2.     public static void main(String[] args) 
  3.     { 
  4.         //construct objects here 
  5.         ... 
  6.     } 

    main方法不对任何对象进行操作。事实上,在启动程序时还没有任何一个对象。静态的main方法将执行并创建程序所需要的对象。

    提示:每一个类可以有一个main方法。这是一个常用于对类进行单元测试的技巧。例如,可以在Employee类中添加一个main方法:

 
  
  1. class Employee 
  2.     public Employee(String n, double s, int year, int month, int day) 
  3.     { 
  4.         name = n; 
  5.         salary = s; 
  6.         GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day); 
  7.         hireDay = calendar.getTime(); 
  8.     } 
  9.     ... 
  10.     public static void main(String[] args)    //unit test 
  11.     { 
  12.         Employee e = new Employee("Romeo"500002003331); 
  13.         e.raiseSalary(10); 
  14.         System.out.println(e.getName() + " " + e.getSalary()); 
  15.     } 
  16.     ... 

    如果想要独立地测试Employee类,只需要执行

 
  
  1. java Employee 

    如果雇员类是大型应用程序的一部分,就可以使用下面这条语句运行程序

 
  
  1. java Application 

    并且Employee类的main方法永远不会被执行。

    例4-3中的程序包含了Employee类的一个简单版本,其中有一个静态域nextId和一个静态方法getNextId。这里将三个Employee对象写入数组,然后打印雇员信息。最后,打印出下一个可用的员工标识码来作为对静态方法使用的演示。

    需要注意,Employee类也有一个静态的main方法用于单元测试。试试运行

 
  
  1. java Employee 

 
  
  1. java StaticTest 

执行两个main方法。

 
  
  1. //** 
  2.   * This program demonstrates static methods 
  3.   * @Version 1.01 2012-11-13 
  4.   * author Wucg 
  5.   */ 
  6. public class StaticTest 
  7. public static void main(String[] args)
  8. {
  9.      //fill the staff array with three Employee objects 
  10.      Employee[] staff = new Employee[3]; 
  11.  
  12.      staff[0] = new Employee("Tom"40000); 
  13.      staff[1] = new Employee("Dick"60000); 
  14.      staff[2] = new Employee("Harry"65000); 
  15.  
  16.      // print out information about all Employee objects 
  17.      for(Employee e : staff) 
  18.      { 
  19.          e.setId(); 
  20.          System.out.println("name=" + e.getName() + ", id=" + e.getId() + ", salary=" + e.getSalary()); 
  21.      } 
  22.  
  23.      int n = Employee.getNextId();    // calls static method 
  24.      System.out.println("Next available id = " + n); 
  25. }
  26.  
  27. class Employee 
  28.     public Employee(String n, double s) 
  29.     { 
  30.         name = n; 
  31.         salary = s; 
  32.         id = 0
  33.     } 
  34.  
  35.     public String getName() 
  36.     { 
  37.         return name; 
  38.     } 
  39.  
  40.     public double getSalary() 
  41.     { 
  42.         return salary; 
  43.     } 
  44.  
  45.     public int getId() 
  46.     { 
  47.         return id; 
  48.     } 
  49.  
  50.     public void setId() 
  51.     { 
  52.         id = nextId;    // set id to next available id 
  53.         nextId++; 
  54.     } 
  55.  
  56.     public static int getNextId() 
  57.     { 
  58.         return nextId;    // returns static field 
  59.     } 
  60.  
  61.     public static void main(String[] args)    // unit test 
  62.     { 
  63.         Employee e = new Employee("Harry"50000); 
  64.         System.out.println(e.getName() + " " + e.getSalary()); 
  65.     } 
  66.  
  67.     private String name; 
  68.     private double salary; 
  69.     private int id; 
  70.     private static int nextId = 1