Tuesday 18 April 2017

Difference between final vs finally and finalize in Java



Difference between final vs finally and finalize in Java


The final, finally, and finalize are one of the three confusing keywords, modifiers and methods in Java. They sounds similar but they are for different purpose. For example, final keyword is a modifier in Java. When you use final keyword with a class it becomes a final class and no one can extend this e.g. String is final in Java. When you use the final modifier with method than it cannot be overridden in subclass, and when you use the final keyword with variable, it become a constant i.e. its value cannot be changed once assigned. On the other hand, finally is a keyword related to exception handling in Java. It's often used with try block and it's part of try, catch and finally trio. A finally block can be used with or without catch block in Java and its guaranteed to be always executed, irrespective of what happens inside try block. This is the reason finally block is used to do cleanup and free resources.


The third one i.e. finalize() is a method in java.lang.Object class and part of Java's garbage collection process. A garbage collector is supposed to call the finalize() method before reclaiming memory from a dead object in Java.

Remember, it's supposed to be called but its not guaranteed hence it's not reliable and thus there is no practical use of finalize() method in Java. This was the basic difference between final, finalize() and finally in Java.  Anyway, let's see some more points to understand their differences better in Java.





Difference between final vs finally vs finalize in Java
The difference given in first paragraph about final, finally and finalize() method is sufficient from an interview point of view but it's better to know some more differences if you want to impress the interviewer or want to prepare better for follow-up questions. Let's see couple of more points to learn this concept better.


1) First and foremost, even though their name sounds similar, final, finally and finalize() are completely different from each other. There is no similarity in their function, whatever is, its only on their names.


2) The final keyword can be used with a class, method or variable in Java. A final class is not extensible in Java, a 
final method cannot be overridden, and a final variable cannot be changed. You can make a class Immutable in Java by using the final keyword. Similarly you can prevent overriding by using the final keyword with method and you can declare static final method to represent constant values.


3) You need either a catch or a finally block with try block in Java. Main advantage of the finally block is that its guaranteed to be executed even if your try block throws an exception. There is only one scenario when finally block doesn't get executed, when 
JVM crashes or you call System.exit() or Runtime.exit() from try block.

See  
Core Java Volume 1 - Fundamentals to learn more when finally block doesn't execute and what happens during JVM crash e.g. whether shutdown hook runs or not.

Difference between final, finalize and finally method in Java


A finally block can also override return values, for example if a try block return 10 and finally block return 20 then method will return 20. Any value returned form finally block will be used.

Even though finally block is guaranteed to be executed, many developer makes mistake of 
closing streams in finally block without realizing that the close() method itself can throw exception and can left other streams opened, that's why right way to close the stream in Java is to use a separate try-catch or try-finally block inside finally block in Java, it's also an exception handling best practice in Java, as shown below:


Stream stream = null;
try{
  stream = open();
  // do something with stream
}finally{
  if(stream != null){
    try{ stream.close()}catch{}
  }
}


4) Last, the finalize() method is one of the 7 methods declared inside java.lang.Object class. As per Java specification its supposed to be called on the object before its gets garbage collected. It is supposed to give last chance to the object to do cleanup or resurrect itself, but main problem is that its not guaranteed to be called. Due to this reason, finally block is not useful in Java. Even the great Joshua Bloch's has advised against using finalize method in classic 
Effective Java book, but this quote from Google coding standard effectively summarize the finalize() method in Java.

Difference between final, finally and finalize method in Java


That's all about difference between final, finally and finalize in Java. As I said, all three are completely different from each other, only similarities is that their name sounds similar. final is a modifier which can be used with class, method or variable in Java. finally block is used along with try block in Java and finalize() is a method which is called by Garbage collector just before the object is garbage collected.


No comments:

Post a Comment