Manuals and instructions for everything

why finalize method is protected in java

Showing 1 - 0 of 0 Ans ravi_1229 Answered On : Mar 24th, 2008
Every user defined class extends object class in java. so if the user defined class having some resources to deallocate(such as database connection, file closing, streams closing) the finalize method is overriden in the current class. thats y the finalize method in object class is protected. vimalraj Answered On : Mar 24th, 2008 The fact that class Object declares a finalize method means that the finalize method for any class can always invoke the finalize method for its superclass.

This should always be done, unless it is the programmer s intent to nullify the actions of the finalizer in the superclass. (Unlike constructors, finalizers do not automatically invoke the finalizer for the superclass; such an invocation must be coded explicitly. ) finalize method in java is a special method much like the. is called before Garbage collector reclaim the Object, its last chance for any object to perform cleanup activity i. e. releasing any system resources held, closing connection if open etc.

The main issue with finalize method in Java is it's not guaranteed by JLS that it will be called by Garbage collector or exactly when it will be called, for example, an object may wait indefinitely after becoming method gets called. similarly even after finalize gets called it's not guaranteed it will be immediately collected. Because of above reason it makes no sense to finalize method for releasing critical resources or perform any time critical activity inside finalize.

It may work in development in one JVM but may not work in other JVM. In this Java tutorial, we will see some important points about finalize method in Java, How to use finalize method, what to do and what not to do inside finalize in Java. Btw, the best advice on finalize method is given by none other than Joshua Bloch on his timeless classic book,. Don't forget to read that after reading this article.

  • Views: 63

why do we use public static void main in java
why do we use interfaces in java
why do we use interface in java
why do we use constructors in java
why do we need interfaces in java
why do we need interface in java
why do we use public static void main in java