File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

question from marcus green

 
samdeep aarzoo
Ranch Hand
Posts: 160
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 195
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic