aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Q on finalize() method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Q on finalize() method" Watch "Q on finalize() method" New topic
Author

Q on finalize() method

Reshma Shanbhag
Ranch Hand

Joined: Sep 17, 2002
Posts: 203
Hi All,
Plz help me with a right answer for the question pasted below.
If a Runtime Exception is thrown in the finalize method -
1. The running application crashes.
2.The exception is simply ignored and the object is garbage collected.
3.The exception is simply ignored, but the object is not garbage collected.
4.The Exception causes the JVM to crash.
Ans is 2.
My justification -
According to me the option '3' is right. I agree to statement that 'exception is simply ignored'. When the finalize method is invoked for the first time the exception is thrown, the exception is ignored & the object garbage collection exit's. Next time the garbage collector runs it will not invoke the finalize() method and the object could be garbage collected.
Thanks,
Reshma


SCJP 1.4, SCWCD 1.4
Rashmi Tambe
Ranch Hand

Joined: Aug 07, 2001
Posts: 418
According to me the option '3' is right. I agree to statement that 'exception is simply ignored'. When the finalize method is invoked for the first time the exception is thrown, the exception is ignored & the object garbage collection exit's. Next time the garbage collector runs it will not invoke the finalize() method and the object could be garbage collected.

The exception is simply ignored. thats right...however, the object is garbage collected.unless u resurrect the object again, there no reason for the garbage collector not to collect it. It'll certainly do it.
regards,
Rashmi
Reshma Shanbhag
Ranch Hand

Joined: Sep 17, 2002
Posts: 203
Hi Rashmi
This is what the javadoc mentions

Any exception thrown by the finalize method causes the finalization of this object to be halted, but is otherwise ignored.

reshma
[ March 21, 2003: Message edited by: Reshma Pai ]
sharana sharana
Greenhorn

Joined: Mar 14, 2003
Posts: 24
hi reshma ,
this is is y i think answer 2 is right
As stated in JLS,
If a class does not override method finalize of class Object (or overrides it in only a trivial way)then if instances of such a class become unreachable, they may be discarded immediately rather than made to await a second determination that they have become unreachable.
the finalizable object was reachble only by the thread which invoked finalize() method of the Object.Becuz of exception the Object is abruptly completed and hence BECOMES UNREACHABLE , as JVM says finalize() method of an Object will never be invoked more than once in its life time.

If the Java virtual machine detects that a finalized object has become unreachable, it may reclaim the storage occupied by the object because the object will never again become reachable .


sharana
sharana sharana
Greenhorn

Joined: Mar 14, 2003
Posts: 24
oops typo!!!in above posting - so reposting it
hi reshma ,
this is is y i think answer 2 is right
As stated in JLS,
If a class does not override method finalize of class Object (or overrides it in only a trivial way)then if instances of such a class become unreachable, they may be discarded immediately rather than made to await a second determination that they have become unreachable.
the finalizable object was reachble only by the thread which invoked finalize() method of the Object.Becuz of exception the finalize() method is abruptly completed and hence BECOMES UNREACHABLE [ since oonly thread which has accessing to it gaveup ]. JVM says finalize() method of an Object will never be invoked more than once in its life time.

If the Java virtual machine detects that a finalized object has become unreachable, it may reclaim the storage occupied by the object because the object will never again become reachable .
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
None of the answers are correct. As Reshma quoted above, the exception is ignored but the finalization is halted. The garbage collection process can continue even if finalization is halted. However, there is no guarantee that the garbage collector will complete the process. For example, if a higher priority thread is ready to run then the garbage collection process might not be completed.
It is correct to say that the exception would cause the finalization process to be halted, but it is not possible to guarantee that the garbage collection process will or will not run.
The real exam will test your knowledge of what is guaranteed. It will not test your knowledge of might or might not happen.


Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Reshma Shanbhag
Ranch Hand

Joined: Sep 17, 2002
Posts: 203
Thanks a lot Dan for helping me out.

Reshma
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Q on finalize() method