Source of EmployeeTest.java


  1: // Fig. 8.13: EmployeeTest.java
  2: // Static member demonstration.
  3: 
  4: public class EmployeeTest 
  5: {
  6:    public static void main( String args[] )
  7:    {
  8:       // show that count is 0 before creating Employees
  9:       System.out.printf( "Employees before instantiation: %d\n",
 10:          Employee.getCount() );
 11: 
 12:       // create two Employees; count should be 2
 13:       Employee e1 = new Employee( "Susan", "Baker" );
 14:       Employee e2 = new Employee( "Bob", "Blue" );
 15:    
 16:       // show that count is 2 after creating two Employees
 17:       System.out.println( "\nEmployees after instantiation: " );
 18:       System.out.printf( "via e1.getCount(): %d\n", e1.getCount() );
 19:       System.out.printf( "via e2.getCount(): %d\n", e2.getCount() );
 20:       System.out.printf( "via Employee.getCount(): %d\n", 
 21:          Employee.getCount() );
 22:    
 23:       // get names of Employees
 24:       System.out.printf( "\nEmployee 1: %s %s\nEmployee 2: %s %s\n\n",
 25:          e1.getFirstName(), e1.getLastName(), 
 26:          e2.getFirstName(), e2.getLastName() );
 27: 
 28:       // in this example, there is only one reference to each Employee,
 29:       // so the following two statements cause the JVM to mark each 
 30:       // Employee object for garbage collection
 31:       e1 = null; 
 32:       e2 = null;  
 33: 
 34:       System.gc(); // ask for garbage collection to occur now
 35: 
 36:       // show Employee count after calling garbage collector; count 
 37:       // displayed may be 0, 1 or 2 based on whether garbage collector
 38:       // executes immediately and number of Employee objects collected
 39:       System.out.printf( "\nEmployees after System.gc(): %d\n", 
 40:          Employee.getCount() );
 41:    } // end main
 42: } // end class EmployeeTest
 43: 
 44: 
 45: 
 46: /**************************************************************************
 47:  * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
 48:  * Pearson Education, Inc. All Rights Reserved.                           *
 49:  *                                                                        *
 50:  * DISCLAIMER: The authors and publisher of this book have used their     *
 51:  * best efforts in preparing the book. These efforts include the          *
 52:  * development, research, and testing of the theories and programs        *
 53:  * to determine their effectiveness. The authors and publisher make       *
 54:  * no warranty of any kind, expressed or implied, with regard to these    *
 55:  * programs or to the documentation contained in these books. The authors *
 56:  * and publisher shall not be liable in any event for incidental or       *
 57:  * consequential damages in connection with, or arising out of, the       *
 58:  * furnishing, performance, or use of these programs.                     *
 59:  *************************************************************************/