What is 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 couple of good questions e.g. how to avoid deadlock in Java, How get() method of HashMap works and one of the puzzle which is based on recursion. In short final keyword can be used along with variable, method and class and has 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
As 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 it's value, it will act like a constant. final variables are initialized at the time of creation except in case of blank final variable which is initialized in Constructor. If you make a method final in Java, you can not override it in sub class . 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 sometime required due to security reason, This is one of the reason Why String is final in Java. In short final is not related at all with either finally or finalize keyword. final keyword also help to write Immutable classes which are critical for designing thread-safe multi-threading system and reducing amount of synchronization. I would suggest to see What is final in Java for more information about 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 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 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 on most cases, it also gives birth to some tricky Java questions where finally doesn't execute e.g. returning value from finally block, calling System.exit from try block etc. finally block always execute, except in 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 object to perform any cleanup but since its 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 different keyword, they are used for different purpose. 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 :