Last Updated: 17 December, 2023
In Java, final, finally, and finalize are three distinct concepts, although their names might seem similar at first glance. Each of these terms has different uses and functionality in Java that we will see in this tutorial and understand with the help of examples.
In Java, final and finally are keywords, but finalize is a method of the java.lang.Object class.
final is a keyword or reserved word that can be used in Java programming to restrict or set the limitations of uses for variables, methods, and classes.
The final keyword is a non-access modifier that is used with the following for different purposes:
Let's see the examples below of uses of final keyword with variable, method and class in Java.
Output
Application Fee (INR) : 500.0
Examination City : BANGALORE/BENGALURU
Age Limit : 18
Min Qualification (General) : GRADUATION
Min Qualification (RESERVED) : INTERMEDIATE
Output
Parent class display() method
Output
LoginController - login() method
Finally is a keyword that is used to create a block that contains a set of crucial statements. Finally block is attached to try-catch blocks.
finally block is always executed, whether an exception occurs or not. Therefore, it contains all the necessary statements that need to be executed, such as closing a connection or closing a file, etc.
Output
Executing try block...
Executing finally block...
Rest of the code after try/catch blocks...
The finalize() method is a java.lang.Object class method: This is a special method in Java that is called by the garbage collector just before an object is reclaimed. This method can be overridden to perform cleanup tasks before an object is destroyed. However, its use is not recommended, as there is no guarantee when or if the garbage collector will call the finalize() method. Instead, we should use other mechanisms like ‘try-with-resources’ or explicit resource management for cleanup tasks.
Output
Main method ended =====
There are the following differences between final, finally and finalize as given in the below table:
Key | final | finally | finalize |
Type | Keyword | Keyword | Method |
Uses | Used to restrict or limit the uses of variables, methods, and classes. | Used to create a block of code that follows a try or catch block. | Used just before an object is destroyed and can be called just prior to object creation. |
Applicable | With variables, methods, and classes | Associated with try-catch block in exception handling. | Used with the objects. |
Functionality |
|
The finally block closes all the resources that were opened as a part of the try block. | Prior to destroying an object, the finalize method performs cleaning activities. |
Execution | Always during initialization/declaration | Always after try-catch block | When garbage collector decides |
Recommendation | Preferred for immutability and performance | Use for resource management | Avoid for cleanup |
Remember, final and finally are proactive measures, while finalize is a reactive one and should be used sparingly.
That's all guys, hope this Java article is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com