aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Regarding Finalize method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Regarding Finalize method" Watch "Regarding Finalize method" New topic
Author

Regarding Finalize method

B pandoo
Ranch Hand

Joined: Nov 14, 2006
Posts: 37
1)The finalize method is invoked by JVM atmost once in its life cycle.
2)if finalize method is overridden,JVM will definitely invoke it.
3)Exceptions thrown by finalize() is ignored by JVM.
4)Calling System.gc will definitely call finalize().
5)the finalize method may throw checked exceptions
Which is true? -- is the Question

I know that option 1 and 3 are correct.
i want to clarify one thing.
1)i know that finalize() can be overriden,but not sure that JVM will definitely invoke it.please confirm it.
2)the finalize method throws exception of type Throwable.any checked exception is a subclass of Exception, and Exception itself is a subclass of Throwable.i believe option 5 is also correct.tell me if i am wrong.
Satish Kota
Ranch Hand

Joined: Feb 08, 2006
Posts: 88
1)The finalize method is invoked by JVM atmost once in its life cycle.
ANS: Yes.
2)if finalize method is overridden,JVM will definitely invoke it.
ANS: No, Its not necessary to override finalize() method in order to be called by GC.

3)Exceptions thrown by finalize() is ignored by JVM.
ANS: Yes. This is from Sun API:

If an uncaught exception is thrown by the finalize method, the exception is ignored and finalization of that object terminates.


4)Calling System.gc will definitely call finalize().
ANS: No, Not necessary. If an obejct is still being referenced by some objects, JVM will not execute finalize().

5)the finalize method may throw checked exceptions
ANS: Yes, the signature of finalize() says protected void finalize() throws Throwable. And since all exceptions and errors extend Throwable, finalize() can throw any kind of exception.


SCJP 5.0 77%
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Hi ranchers,

yes, the Exception will be ignored.

prints "Watch out for Banana skins!"
but no Exception is thrown.


Point two: if finalize method is overridden,JVM will definitely invoke it.
No, it is not guarantied that finalize is executed, beeing overridden or not.


Yours,
Bu.


all events occur in real time
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8883
    
    5
Hi B -

It's totally cool to post mock questions on this forum. However, when you post mock questions you should do two things:

1 - mention the source of the question
2 - copy the question EXACTLY!!!

A small change in the phrasing of a question can COMPLETELY CHANGE its meaning!!


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Regarding Finalize method