| Author |
question from marcus green
|
samdeep aarzoo
Ranch Hand
Joined: Jun 09, 2005
Posts: 160
|
|
What will happen when you compile and run the following code? class Box{ private int iVolume; Box(int iVolume){ this.iVolume=iVolume; } public void finalize(){ System.out.println("finalizing a Box"); } } public class EdGrundy{ boolean stop = false; public static void main( String argv[]){ new EdGrundy(); } EdGrundy(){ while(stop==false){ new Box(99); } } }
answer given Compilation and eventually multiple outputs of "finalizing a Box" can anybody explain it how finalize method is invoked
|
 |
Joe Sondow
Ranch Hand
Joined: Apr 10, 2005
Posts: 195
|
|
|
finalize() is invoked when the garbage collector collects a Box object that cannot be accessed by any user thread. The program runs an endless loop in which Box objects are created but no references to them are kept, so they quickly become eligible for garbage collection. As soon as the GC runs, it will collect lots of abandoned Box objects, and it will invoke the finalize() method of each one.
|
SCJA 1.0 (98%), SCJP 1.4 (98%)
|
 |
 |
|
|
subject: question from marcus green
|
|
|