Source of Employee.java


  1: // Fig. 8.12: Employee.java
  2: // Static variable used to maintain a count of the number of 
  3: // Employee objects in memory.
  4: 
  5: public class Employee 
  6: {
  7:    private String firstName;
  8:    private String lastName;
  9:    private static int count = 0; // number of objects in memory
 10: 
 11:    // initialize employee, add 1 to static count and 
 12:    // output String indicating that constructor was called
 13:    public Employee( String first, String last )
 14:    {
 15:       firstName = first;
 16:       lastName = last;
 17: 
 18:       count++;  // increment static count of employees
 19:       System.out.printf( "Employee constructor: %s %s; count = %d\n",
 20:          firstName, lastName, count );
 21:    } // end Employee constructor
 22: 
 23:    // subtract 1 from static count when garbage 
 24:    // collector calls finalize to clean up object;
 25:    // confirm that finalize was called
 26:    protected void finalize()
 27:    {
 28:       count--; // decrement static count of employees
 29:       System.out.printf( "Employee finalizer: %s %s; count = %d\n",
 30:          firstName, lastName, count );
 31:    } // end method finalize
 32: 
 33:    // get first name
 34:    public String getFirstName() 
 35:    { 
 36:       return firstName; 
 37:    } // end method getFirstName
 38: 
 39:    // get last name
 40:    public String getLastName() 
 41:    { 
 42:       return lastName; 
 43:    } // end method getLastName
 44: 
 45:    // static method to get static count value
 46:    public static int getCount() 
 47:    { 
 48:       return count; 
 49:    } // end method getCount
 50: } // end class Employee
 51: 
 52: 
 53: 
 54: /**************************************************************************
 55:  * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
 56:  * Pearson Education, Inc. All Rights Reserved.                           *
 57:  *                                                                        *
 58:  * DISCLAIMER: The authors and publisher of this book have used their     *
 59:  * best efforts in preparing the book. These efforts include the          *
 60:  * development, research, and testing of the theories and programs        *
 61:  * to determine their effectiveness. The authors and publisher make       *
 62:  * no warranty of any kind, expressed or implied, with regard to these    *
 63:  * programs or to the documentation contained in these books. The authors *
 64:  * and publisher shall not be liable in any event for incidental or       *
 65:  * consequential damages in connection with, or arising out of, the       *
 66:  * furnishing, performance, or use of these programs.                     *
 67:  *************************************************************************/