«Back to Home

Core Java

Topics

Differentiate Between Final, Finally And Finalize In Java

Differentiation between final, finally and finalize

Let’s see the differences between final, finally and finalize, which are,
 
Final

Final is a keyword, which is used to apply restrictions on class, method and variable. If we declare any class as a final, the class can't be inherited. If we declare any method as a final, the method can't be overridden and if we declare any variable as a final, the variable value can't be changed and initialized only once in Java.
 
Let’s see an example of final, given below.
 
Code
  1. public class Employee {  
  2.     final int empId = 101//final variable  
  3.     void work() {  
  4.         empId = 211// Compile time error  
  5.     }  
  6.     public static void main(String args[]) {  
  7.         Employee obj = new Employee();  
  8.         obj.work();  
  9.     }  
  10. }  
34
 
In the example, shown above, we declare the final variable and initialize it at the time of the declaration. Hence, we cannot initialize it again or we cannot change its value. It will be constant.
 
Finally

Finally is a block, which is used to place the important code. It always executes whether an exception is handled or not. It must be followed by try or catch block. Finally, block allows us to run any cleanup-type statements, which we want to execute, no matter what happens in the protected code such as closing a file, closing connection etc.
 
Let’s see an example of final, given below.
 
Code
  1. package exceptionHandling;  
  2. public class FinallyExample2 {  
  3.     public static void main(String args[]) {  
  4.         try {  
  5.             int num = 36 / 0;  
  6.             System.out.println(num);  
  7.         } catch (NullPointerException e) {  
  8.             System.out.println(e);  
  9.         } finally {  
  10.             System.out.println("finally block....");  
  11.         }  
  12.         System.out.println("Rest code...");  
  13.     }  
  14. }  
35

Output

36

In the above example, we can see that exception occurred and not handled but finally block is always executes whether exception is handled or not.
 
Finalize

Finalize is a method, which is used to perform clean up processing before an object is garbage collected, finalize() method calls at the runtime and we can write the system resources, which released code in finalize() method before getting garbage collected in Java.
 
Let’s see an example of finalize, given below.
 
Code
  1. package exceptionHandling;  
  2. public class FinalizeExample {  
  3.     public void finalize() {  
  4.         System.out.println("finalize method called");  
  5.     }  
  6.     public static void main(String[] args) {  
  7.         FinalizeExample f = new FinalizeExample();  
  8.         f = null;  
  9.         System.gc();  
  10.     }  
  11. }  
37

Output

38

In the example, shown above, we can see that finalize() method calls at the runtime before an object is garbage collected.
 
Summary

Thus, we learnt Final is a keyword, Finally is a block, Finalize is a method and also learnt their differences in Java with the example.