• 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

Finalize() method question

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a whizlab question:



The question is to ask what are the possible results of compile and run the following code.

The answer is A.Print 10
B.Print 10 20 10

So my question is that for answer B, is it supposed to be 10 10 20?
 
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Wonkty,

Java doesn't guaranteed that objects will be garbage collected in the same order as the sequence in which they become eligible. So it's possible that t2 will be garbage collected (and finalized) before t1.

In fact, as Option A illustrates, Java may not even garbage collect the objects before the JVM exits.
 
wonkty wang
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, it is clear now.
 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When i ran the code i got the output
A.Print 10
and next time when i ran i got this.
B.Print 10 20 10

1. Since t1 explicitly invokes finalize() and when t1=null and System.gc() is invoked the object referenced by t1 will be eligible for garbage collection , But as we know that finalize() is invoked only once by JVM just before garbage collection and if the object is saved in other reference in finalize() and again when the object becomes eligible for GC , JVM will not invoke finalize() because it was already invoked.

So my question is since t1.finalize() invokes finalize() , How come JVM again invokes finalize()?
2. I feel this question is incorrect because of the following reasons
a) System.gc() does not guarantee that the object will be GC'ed , Its just a request. I hope real exam will not have such questions whose output can vary with each execution of the class.
Thanks
Deepak
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Deepak Jain:
So my question is since t1.finalize() invokes finalize() , How come JVM again invokes finalize()?



I think is the same trick thing that calling run() method in a thread. It's different behavior if you use it instead of the JVM.
 
reply
    Bookmark Topic Watch Topic
  • New Topic