• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regarding Finalize method

 
B pandoo
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bert Bates
author
Sheriff
Posts: 8898
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic