• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

question from marcus green

 
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
The overall mission is to change the world. When you've done that, then you can read this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic