The difference in final, finally and finalize is the imp from interview point of view. This is one of the most favorite question of many interviewers for Java freshers as well as for java experienced professionals.
We will see the difference in all these. There are many differences between final, finally and finalize.
Difference in Final, Finally and Finalize
Final – Final is a keyword, which can make the variable, class or method ‘unchangeable’. In other words –
- A variable which is declared as final, it’s value can not be changed once it is initialized.
- A method declared as final can not be overridden or modified in the subclass.
- A class declared as final can not be extended.
You can see the more use of Final in Final Keyword Tutorial.
Finally – finally is a block which is used for exception handling along with try and catch blocks. The finally block always executes when the try block exits. This ensures that the finally block is executed even if an unexpected exception occurs. But finally is useful for more than just exception handling – it allows the programmer to avoid having cleanup code accidentally bypassed by a return, continue, or break. Putting cleanup code in a finally block is always a good practice, even when no exceptions are anticipated.
You can learn more about finally keyword in the Finally Block Tutorial.
Selenium Testing Video Tutorial:
Finalize() – Finalize() is a method. It is protected method of java.lang.Object class . It is inherited to every class you create in java. This method is called by garbage collector thread before an object is removed from the memory. finalize() method is used to perform some clean up operations on an object before it is removed from the memory. You can write system resources release code in finalize() method before getting garbage collected.
So these are the differences between Final, finally and finalize().