316 Object-Based Programming Chapter 8 15 // create
316 Object-Based Programming Chapter 8 15 // create two Employees 16 Employee employee1 = new Employee( “Susan”, “Baker” ); 17 Employee employee2 = new Employee( “Bob”, “Jones” ); 18 19 Console.WriteLine( “nEmployees after instantiation: ” + 20 “Employee.Count = ” + Employee.Count + “n” ); 21 22 // display the Employees 23 Console.WriteLine( “Employee 1: ” + 24 employee1.FirstName + ” ” + employee1.LastName + 25 “nEmployee 2: ” + employee2.FirstName + 26 ” ” + employee2.LastName + “n” ); 27 28 // remove references to objects to indicate that 29 // objects can be garbage collected 30 employee1 = null; 31 employee2 = null; 32 33 // force garbage collection 34 System.GC.Collect(); 35 36 // wait until collection completes 37 System.GC.WaitForPendingFinalizers(); 38 39 Console.WriteLine( 40 “nEmployees after garbage collection: ” + 41 Employee.Count ); 42 } 43 } Employees before instantiation: 0 Employee object constructor: Susan Baker; count = 1 Employee object constructor: Bob Jones; count = 2 Employees after instantiation: Employee.Count = 2 Employee 1: Susan Baker Employee 2: Bob Jones Employee object destructor: Bob Jones; count = 1 Employee object destructor: Susan Baker; count = 0 Employees after garbage collection: 0 Fig. 8.14 Fig. 8.14Fig. 8.FiFi14g. 8.14g. 8.14staticmember demonstration. (Part 2 of 2.) The garbage collector is not invoked directly by the program. Either the garbage collector reclaims the memory for objects when the runtime determines garbage collection is appropriate, or the operating system recovers the memory when the program terminates. However, it is possible to request that the garbage collector attempt to collect available objects. Line 34 uses public static method Collect from class GC (namespace
Please visit Domain Name Hosting services for high quality webhost to host and run your jsp applications.