File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes question from marcus green Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "question from marcus green" Watch "question from marcus green" New topic
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%)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: question from marcus green
 
Similar Threads
Question from Examsimulator
Reg Garbage Collection
GC question
need help
When finalize() will be called?