• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem about understanding of inheritance and finalizer  RSS feed

 
Laurence Yip
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I have a problem about understanding of inheritance and finalizer



The output is :


if i remove the code snippet for finalizer guardian, the output would become....


For my basic java knowledge, since child1, child2 and parent1 objects will become out of scope after calling doIt() method and they will also become eligible to be collected from VM after calling System.gc() in main().
But i don't understand what the scenario working behind the outputs especially the differences between the two outputs when taking out 'finalizer guardian', anyone can help!?

thanks in advance!

Rgds,

Laurence
 
Mohamed Sanaulla
Bartender
Posts: 3185
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The guardian objects though have a reference are eligible for garbage collection because there is no live reference from the main stack to reach the guardian objects. These objects are called island of isolation. And hence when you include the guardian reference you see its finalize() being invoked.

Please visit this thread for more details on Island of Isolation.
 
Laurence Yip
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Sanaulla,

Thanks for your reply first!

But i still found confused and suppose i amended the doIt() as follows:


provided that guardian stays intact in the Parent class, the corresponding output becomes:


My first wonder is how come Parent class's guardian's finalizer is invoked prior to Child class's finalizer when child1 falls out of scope after calling doIt()!? Sorry for my poor insight for sequence of parent-child finalizer invocation!

Thanks a lot again!

Laurence
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Is this code a cut and paste? Just eyeballing the code, there are stuff that should not compile.

Henry
 
Laurence Yip
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Henry,

i post it again....it works....


Laurence
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Laurence Yip wrote:
My first wonder is how come Parent class's guardian's finalizer is invoked prior to Child class's finalizer when child1 falls out of scope after calling doIt()!? Sorry for my poor insight for sequence of parent-child finalizer invocation!


First, out-of-scope applies only to the reference variable. For the objects, they both became unreachable. And it doesn't matter how (when) they became unreachable -- since the Garbage Collector doesn't know the order anyway. It merely finds objects that are no longer reachable, and finalize them. So, trying to understand an order to finalization (for two different objects) doesn't make sense.

Henry
 
Rajdeep Biswas
Ranch Hand
Posts: 231
1
Eclipse IDE Java Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In agreement with Henry. I think we can be more concerned with nullifying references in finalize block, or, if the case may be, reassign the objects ;-) The order in which the objects are accessed is a JVM specific implementation.
 
Laurence Yip
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,

glad to see so many replies....greatly appreciate yours helps....and hope to see you all again in next question soon....Laurence
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Laurence Yip wrote:glad to see so many replies....greatly appreciate yours helps....and hope to see you all again in next question soon....Laurence

And I hope you've got the general trend of those replies:

Avoid finaliisers.

Except in rare circumstances, they usually indicate either:
a. A flaw in your design.
or
b. A misunderstanding of the way Java works.

If you can explain easily, to an experienced Java-ite, why you think you need them, by all means go ahead.
But not otherwise.

Winston
 
Ulf Lindqvist
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Laurence Yip wrote:
finalizer


See "Finalizer is not your friend",

http://www.ibm.com/developerworks/java/library/j-jtp01274/index.html
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!