This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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.
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.