How Is Finalization Performed?

The example of Listing 7.1 makes use of the finalize() method of the Garbage objects to indicate for which objects the garbage collector has initiated the garbage-collection process. The garbage collector invokes an object's finalize() method when it determines that the object has become unreachable.

The finalize() method is defined in the Object class. However, the Object implementation of finalize() performs no action. Classes override finalize() to perform cleanup prior to garbage collection. For example, a networking object might close down a TCP connection prior to garbage collection.

If an object becomes unreachable, the object's finalize() method is not guaranteed to be invoked. However, if an object does ...

Get Sun Certification Training Guide (310-025, 310-027): Java™ 2 Programmer and Developer Exams 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.