Difference between final, finally and finalize method in Java

What is the difference between final, finally and finalize method is asked to my friend in a Java interview with one of the US based Investment bank. Though it was just a telephonic round interview, he was asked a couple of good questions e.g. how to avoid deadlock in Java, How to get() method of HashMap works and one of the puzzles which are based on recursion. In short final keyword can be used along with variable, method and class and has a different meaning for all of them. finally is another Java keyword which is used in Exception handling along with try, catch, throw and throws. finalize() is a special method in Java which is called by Garbage Collector before reclaiming GC eligible objects. In this Java interview questions article, we will compare final vs finally vs finalize and highlight some important difference between final, finally and finalize method in Java.

final vs finally vs finalize in Java

Difference between final vs finally vs finalize in JavaAs I said earlier final keyword can be used along with variable, method and Class in Java. If you make a variable final, you can not change its value, it will act like a constant. final variables are initialized at the time of creation except in the case of a blank final variable which is initialized in Constructor. If you make a method final in Java, you can not override it in subclass . 

If you make a class final means it can not be sub classed. Making a class final automatically makes all its method final and this is sometimes required due to security reason, This is one of the reasons Why String is final in Java. In short final is not related at all with either finally or finalize keyword. final keyword also helps to write Immutable classes which are critical for designing thread-safe multi-threading system and reducing the amount of synchronization. I would suggest seeing What is final in Java for more information about the final keyword.


Now let's see What is finally in Java? As I said finally is used for exception handling along with try and catch. As per Java programming language’s rule, for exception handling, you at least need either catch or finally block. finally block has a special advantage over catch that its guaranteed to be executed despite whether Exception is thrown or not, this makes it, an ideal place to close system resource e.g. InputStream or OutputStream, which is required to release scarce file descriptor. Closing streams, network connection, database connection in a finally block is good coding practice in Java. By the way from Java 7 you can use try with resource block to close resource automatically. Since finally is guaranteed to be executed in most cases, it also gives birth to some tricky Java questions where finally doesn't execute e.g. returning value from a finally block, calling System.exit from try block etc. finally block always execute, except in the case of JVM dies i.e. calling System.exit() . Again finally is not related to final or finalize in any way.

Now let’s see What is finalize() method, finalize() is called by Garbage collection thread just before collecting eligible Objects. This is the last chance for an object to perform any cleanup but since it's not guaranteed that whether finalize() will be called, its bad practice to keep resource till finalize call. Though you can build a safety net on finalize by double checking scarce resources. See 10 points on finalize method to know more about specific points of finalize().

So, final, finally and finalize all are a different keyword, they are used for different purpose. the only similarity between them is that they are a Java programming language keyword, other than that final, finalize and finally are completely different than each other.

Other Java programming interview questions from Javarevisited :


1 comment :

Anonymous said...

I have seen this question in first round of interview. final is a modifier, finally is used in exception handling and finalize is a method for garbage collection, that's what you need to know.

Post a Comment