AkramAbd

8.12 Static variable used to maintain a count of the number

Mar 6th, 2017
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  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 Employees created
  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. // get first name
  24. public String getFirstName()
  25. {
  26. return firstName;
  27. } // end method getFirstName
  28.  
  29. // get last name
  30. public String getLastName()
  31. {
  32. return lastName;
  33. } // end method getLastName
  34.  
  35. // static method to get static count value
  36. public static int getCount()
  37. {
  38. return count;
  39. } // end method getCount
  40. } // end class Employee
Advertisement
Add Comment
Please, Sign In to add comment