Saturday, 23 May 2015

final Vs finally Vs finalize in Java

What are the differences among final, finally and finalize in java is one question asked a lot in Java interviews. This question is asked more to confuse a candidate as they all sound similar and of course by asking this question you get to assess the knowledge of the candidate about three things final, finally and finalize!

Apart from having ‘final’ in all of these there is no similarity. Though it can be said that primary job of both finally and finalize is to clean up, so there is some similarity in functionality between finally and finalize.

So let’s go through final, finally and finalize to see where they differ and how.

final Vs finally Vs finalize in Java

final – final keyword is used to restrict in some way. It can be used with variables, methods and classes. When a variable is declared as final, its value can not be changed once it is initialized. Except in case of blank final variable, which must be initialized in the constructor.
If you make a method final in Java, that method can’t be overridden  in a sub class.
If a class is declared as final then it can not be sub classed.

finally – finally is part of exception handling mechanism in Java. finally block is used with try-catch block. Along with a try block we can have both catch and finally blocks or any one of them. So we can have any of these combinations try-catch-finally, try-catch, try-finally. finally block is always executed whether any exception is thrown or not and raised exception is handled in catch block or not. Since finally block always executes thus it is primarily used to close the opened resources like database connection, file handles etc.
From Java 7 onwards try with resources provide another way to automatically manage resources.

finalize() - finalize() method is a protected method of java.lang.Object class. Since it is in Object class thus it is inherited by every class. This method is called by garbage collector thread before removing an object from the memory. This method can be overridden by a class to provide any cleanup operation and gives object final chance to cleanup before getting garbage collected.

protected void finalize() throws Throwable
{
    //resource clean up operations
}

Please note that it is not a good idea to rely on finalize() method for closing resources as there is no guarantee when finalize() method will be called by Garbage collector.

That's all for this topic final Vs finally Vs finalize. If you have any doubt or any suggestions to make please drop a comment. Thanks!



Related Topics

  1. Difference between Checked exception & Unchecked exception
  2. Difference between throw and throws
  3. Multi catch statement in Java 7
  4. try-with-resources in Java 7
  5. Java Exception Handling interview questions

You may also like -

1 comment:

  1. Thanks for your comment on my blog post http://javarevisited.blogspot.sg/2011/07/jdk7-multi-cache-block-example-tutorial.html. It seems you have also started a good blog. Keep it up.

    And one more point, don't rely on finalize() for any cleanup operation, its not guaranteed. To learn more about it see my post Difference between final, finally and finalize in Java

    ReplyDelete