Java Final vs Finally vs Finalize

The final word is used in Java to limit the modification of variables, methods and classes. This block is applied in Java to ensure the constant execution of a section of code whether an exception is invoked or not. The finalizer is a Java method which performs cleaning operations before garbage collection of an object.

Difference between final, finally and finalize in Java:

  • The Java keywords used in exception handling are Final, Last, and Complete. Each of these keywords has several uses.
  • The primary distinction between final, finally, and finalize is that final is an object class function, finally is an exception handling block, and finalize is an access modifier.

Additionally, there are several distinctions between final, final, and finalized. The lists under “Final, Complete, and Finalization” are as follows:

Definition of Final in Java:

The final element is the keyword and access modifier used for applying restrictions to a class, method or variable.

Java final Example:

public class Techvidvan{  
      final int age = 18;  
    void display() {  
    age = 55;  
    }  
    public static void main(String[] args) {  
    Techvidvan obj = newTechvidvan();   
    obj.display();  
    }
}

Java finally Example:

In the following example, we will see how Java throws an exception that is handled by a catch block. After the trycatch block, the final block will be executed. Otherwise, the rest of the code’s being run in normal fashion.

Java finally Example:

public class Techvidvan{    
      public static void main(String args[]){   
      try {    
        System.out.println("Inside try block");   
       int data=25/0;    
       System.out.println(data);    
      } 
      catch (ArithmeticException e){  
        System.out.println("Exception handled");  
        System.out.println(e);  
      }   
      finally {  
        System.out.println("finally block is always executed");  
      }    
      System.out.println("rest of the code...");    
      }    
    }

Java finalize Example:

public class Techvidvan{    
     public static void main(String[] args)     
    {     
        Techvidvan obj = new Techvidvan();       
        System.out.println("Hashcode is: " + obj.hashCode());           
        obj = null;    
        System.gc();     
        System.out.println("End of the garbage collection");     
    }      
    protected void finalize()     
    {     
        System.out.println("Called the finalize() method");     
    }     
}
S.No key Final Finally Finalize
1. Definition The last element is a key and access modifier used to set limit values ​​for classes, methods or variables. Finally, the Java Exception Handling block If no exception occurs, special code is executed. Finalize is a method in Java that is used to perform cleanup just before an object is collected.
2. Applicable to Use the last keyword to specify classes, methods and variables. Finally, the block relates to a try and retrieve block as far as exemption handling is concerned.  The finalize() method is used for handling objects.
3. Functionality
  • Once declared, the final variable is immutable and cannot be changed.
  • The final variable is not changeable as soon as you declare it. The final method cannot be changed by a sub class. No one can inherit the last class.
  • Once declared, the final variable is immutable and cannot be changed.
  • The final variable is not changeable as soon as you declare it. The final method cannot be changed by a sub class. No one can inherit the last class.
Before destroying the object, the completion method performs a cleansing operation on it.
4. Execution We only execute the last method when we call it. The last block will be executed once the trycatch block is completed. Its implementation does not depend on the exception. The finalize method is executed just before the object is destroyed.

How does the finalize() Method Work with Garbage Collection?

  • The JVM calls the garbage collector to remove unreferenced items, as explained above.
  • It calls the finalize() method to perform a clean operation, and the garbage collector destroys the object after determining objects that have no references or references.

Let’s see a code to understand this:

public class Demo
{
    public static void main(String[] args)
    {
        String a = "Hello World!";
        a = null; 
    }
}

If Hello World! is the value of a String object in this program. It refers to a string object. If that value is not valid, it does not have a connection. Therefore, it is entitled to collect waste. Before you destroy your object, the garbage collector will call finalize() to clean it up.

Conclusion

Garbage collection is done automatically in Java, which is handled by the JVM and uses the finalize method in Java to release the resources of the object to be destroyed.

The GC calls the finalize() method only once unless an exception is thrown by the finalize method or if the objects are not revived from final() when the garbage collector does not call it again.