Wednesday, November 20, 2019

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 in Java!

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 differences among final, finally and finalize in Java.

final Vs finally Vs finalize in Java

final– final keyword in Java 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.

try {
  // code that may throw an exception
} catch (Exception e) {
  // catch exception
} finally {
  //This block is always executed
  // so close any open resources here
}

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 in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!


Related Topics

  1. Difference Between Checked & Unchecked Exception in Java
  2. Java Exception Handling Tutorial
  3. Multi-Catch Statement in Java Exception Handling
  4. Try-With-Resources in Java Exception Handling
  5. Java Exception Handling Interview Questions And Answers

You may also like-

  1. interface default methods in Java 8
  2. Difference Between Abstract Class And Interface in Java
  3. static import in Java
  4. Inter-thread Communication Using wait, notify And notifyAll - Java Multithreading
  5. Why wait(), notify() And notifyAll() Methods Are in Object Class And Not in Thread Class
  6. How HashMap Internally Works in Java
  7. Difference Between Comparable and Comparator in Java
  8. ReentrantLock in Java concurrency

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