What is the difference between the final, finally, and finalize method is asked to my friend in a Java interview with one of the US-based Investment banks. 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 is used in Exception handling along with try, catch, throw, and throws. finalize() is a special method in Java that 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 differences between the final, finally, and finalize methods in Java.
In this Java interview questions article, we will compare final vs finally vs finalize and highlight some important differences between the final, finally, and finalize methods 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 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 a subclass.
If you make a class final means it cannot be sub-classed. Making a class final automatically makes all its methods final and this is sometimes required due to security reasons, This is one of the reasons Why String is final in Java.
In short, the final is not related at all with either finally or finalize keyword. The final keyword also helps to write Immutable classes which are critical for designing a 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.
If you make a class final means it cannot be sub-classed. Making a class final automatically makes all its methods final and this is sometimes required due to security reasons, This is one of the reasons Why String is final in Java.
In short, the final is not related at all with either finally or finalize keyword. The final keyword also helps to write Immutable classes which are critical for designing a 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 the Java programming language’s rule, for exception handling, you at least need either catch or finally block. finally, the block has a special advantage over catch that it's guaranteed to be executed despite whether an Exception is thrown or not.
This makes it, an ideal place to close system resources like InputStream or OutputStream, which is required to release scarce file descriptors. Closing streams, network connections, database connections 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 like returning a value from a finally block, calling System.exit from try block etc. finally block always executes, except in the case of JVM dies i.e. calling System.exit() .
This makes it, an ideal place to close system resources like InputStream or OutputStream, which is required to release scarce file descriptors. Closing streams, network connections, database connections 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 like returning a value from a finally block, calling System.exit from try block etc. finally block always executes, 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 the 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 resources till finalize call.
And, if you like to see them in a list format, here is a nice list of what is the purpose of final, finally, and finalize in Java and how they differ from each other:
Though you can build a safety net on finalize by double-checking scarce resources. See 10 points on the finalize method to know more about specific points of finalize().
So, final, finally, and finalize all are different keywords, they are used for different purposes. 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 :
2 comments :
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.
"If you make a variable final, you can not change its value" I think this statement should be changed to "If you make a variable as final, it means that you cannot assign a different object reference to that variable". If it is a final object then definitely we can change the content of that object but we cannot make that reference point to other object.
Post a Comment