Final, Finally and Finalize in java

In the previous post ,we have seen the difference between throw and throws keywords. In this post, we will see the difference between final, finally and finalize keywords.

Let’s start with final keyword.

Final keyword in java:

Final keyword is used to make the class, method and variable restricted which can not be modified once declared.

  1. If apply on class, class can not be inherited.
  2. If apply on method, method can not be overridden.
  3. If apply on a variable, variable’s value can not be changed.

Finally block in java:

A final block can be associated with a try block. There are few points which should know about finally block.

1. Finally block will be associated with a try block. Even if try does not have a catch block, finally can be associated with this.

2. The code within finally block will be always be executed , even if exceptions occur or not.

3. It is not mandatory to include finally block. But if we do, it will be executed regardless the exception handled in try-catch block.

4. In normal execution, finally block will be executed after try block. But if exception occur, first catch block will be executed and then finally block will be executed.

5.  It makes sense to have the clean up code in the finally block, like closing a file or data connection. In any situation, if exception occurs during file handling or database operations , the code inside finally block should close the file and database connection.

6. If we have transfer statements like return, break or continue in try-catch blocks, even though finally block will be executed.

Is there any possibility that finally block will not be executed? 

Yes, in some circumstances like as give below.

  • Exception occurred in finally block.
  • System.exit() method used to forcefully exit.
  • Any fatal error causes the application to abort.
Note: For each try block, there can be multiple catch block, but only one finally block.
In the above example, when exception occurs catch block is executed along with finally block.


 

Finalize method in java:

There are few points to know about finalize.

  1. finalize() is a method which is defined in java.lang.Object class, that means it is available to all the classes.
  2. finalize() method is called for clean up process just before the objects are garbage collected.In other words, we can say it is Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.
  3. finalize() is called only once by Garbage collector, if objects can revive themselves then finalize will not be called again.
  4. This method does not return any value.
Output: finalize called
Questions/Suggestions
Have any question or suggestion for us?Please feel free to post in Q&A Forum
Throw and Throws keywords in java
Exception Propagation in java
Shekhar Sharma

Shekhar Sharma

Shekhar Sharma is founder of testingpool.com. He is an automation engineer having more than 8 years of experience who loves troubleshooting in automation and finding innovative ways to solve the problems. His other leisurely activities includes playing harmonica, paintings, sketching portrait, watching movies and travelling etc.

You may also like...

1 Response

  1. August 9, 2015

    […] the previous post, we have seen difference between final, finally and finalize. In this post, we will see what is exception […]