Jtest logo




Contents  Previous  Next  Index

GC.NCF


Never call `finalize()' explicitly

Description

This rule flags code that explicitly calls `finalize()'.

Calling the `finalize()' method explicitly insures that `finalize()' is called, but the Garbage Collector during runtime will call the `finalize()' method again when the object is collected.

Example

 package GC;
 
 class NCF {
     public void finalize() throws Throwable {
         super.finalize();
     }
 }
 class Test {
     void closeTest () throws Throwable {
         _test.finalize(); // this may get called again by Java 
//Virtual Machine
         _test = null;
     }
     private NCF _test = new NCF();
 }

Repair

Create a method to handle the release of the memory, then call this method from a `finalize()' method before calling the `super.finalize()' method.

 public void release() {
     if (!closed) {
         _test.finalize ();
         closed = true;
     }
 }
 public void finalize () throws Throwable {
     release ();
     super.finalize();
 } 

Reference

Warren, Nigel, and Bishop, Philip. Java in Practice. Addison-Wesley, 1999, pp. 110-111.


Contents  Previous  Next  Index

ParaSoft logo
(888) 305-0041 info@parasoft.com Copyright © 1996-2001 ParaSoft