Tracking the Number of Employee Objects That Have Been Created
Our next program declares two classes—Employee
(Fig. F.12) and EmployeeTest
(Fig. F.13). Class Employee
declares private static
variable count
(Fig. F.12, line 9) and public static
method getCount
(lines 36–39). The static
variable count
is initialized to zero in line 9. If a static
variable is not initialized, the compiler assigns it a default value—in this case 0
, the default value for type int
. Variable count
maintains a count of the number of objects of class Employee
that have been created so far.
1 // Fig. F.12: Employee.java 2 // Static variable used to maintain a count of the number of 3 // Employee objects in memory. 4 5 ...
Get Android™ How to Program, Second Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.