Difference between final, finally and finalize in Java

Java supports final, finally and finalize that are all completely used in different aspects. Final is a modifier that can be used with variables,methods and class to put restriction in various context, finalize is a method used to perform cleaning actions before destroying a particular object, and finally is a block used in companion with try (optional with try-catch).

final finally and finalize

Final Keyword

Final is the most common keyword which acts as a non-access modifier in the modern programming languages like C++, Java,etc.

The final keyword can be used along with variables, methods as well as with classes. They can be explained as follows:

  • Final variables

If the final keyword is used with the variable then, that variable becomes a constant.

eg:

final int i=10;

 

  • Final methods

If the method is declared with the final keyword then, that class method cannot be overridden even by its subclass.

eg:

class A{
int y;
// constructor
A(){
y=10;
}
final void display(){

System.out.println(" Value of y ="+y);

}
}


public class Test extends A
{
//error would be occurred
/* void display(){

System.out.println(" Value of y ="+y);
}*/

public static void main(String[] args) {
A obj = new A();
obj.display();
}

}

 

  • Final class

If the class is declared with the final keyword then, it cannot inherit any sub – class.

eg:

final class X{

// statements and methods

}

class Y extends X   // this will generate an error

{

//statements and methods

}

 

Finally Block

Finally is a block which comes into play when working with  Try / Catch block for handling exceptions. The finally block only executes after the try and catch blocks. The interesting fact about the finally block is that it will be executed whether there is an exception or not.

eg:   :

class test {

    public static void main(String[] args) {
 
        int x;
        try{
            x = 100/0;

        }finally {
            System.out.println("I am in finally block");
        }
    }
}

Output:

System.out.println("I am in finally block");

Finalize Method

This method is used to reallocate or free the system’s memory which was acquired by unused objects or unreachable objects. In Java, this whole process comes under the category of garbage collection.

eg:   :

class ABC{

int x=10;

protected void finalize() throws Throwable

{

System.out.println("from the finalize method");

}

}

 

Difference between final, finally and finalize is as follows:

  • GENERIC INFORMATION

If we consider the final, then it is a non-access modifier keyword for variables, methods, and classes used in modern programming; whereas; if we consider finally, then it is a type of block used in the exception handling; whereas; if we consider finalize, then finalize is basically a method of the Object Class.

  • USAGE

When we talk about final, then final is used as a keyword with the variables, methods and classes; whereas; when we talk about finally,

Then finally is a block which is used along with the try/catch block; whereas; when we talk about finalize, then finalize is a method only used with garbage collection, before Garbage collector destroys unreachable objects.

  • DETAILED DESCRIPTION

In case of the final, we have:

Final variables; these variables are constant and cannot be modified,

Final methods; these methods cannot be overridden by the child class’s methods,

Final class; these classes cannot be inherited to any subclass;

Whereas; in case of the finally, the finally block is the last step of the exception handling process; whereas; in case of finalize, the finalize is the method which performs the clean – up tasks which is held by object related to the object’s memory before the actual destruction of the objects.

  • THE EXECUTION

When we talk about final, then final method gets executed only after its call; whereas; when we talk about finally, then finally block is executed only after the execution of the try and catch blocks; whereas; when we talk about finalize, then finalize method is invoked just before the actual deletion or destruction of the object.

 

Key differences:

Basisfinal Keywordfinally Blockfinalize Method
DefinitionIt can be defined as a keyword whose association with a class, variable or method makes them immutableIt can be defined as a block where one should write cleanup code such as closing network connections , database connections and moreIt can be defined as a method that allows us to perform some action before destroying a particular object
Refer toIt is a non-access modifier keyword used with variables, methods and classesIt is a type of block used in the exception handlingIt is basically a method of the Object Class
PurposeMakes a class, method or variable immutableCleanup codes and other important statements are put in this block and executed whether the exception occurs or notReleasing resources that is held by unreachable objects

Leave a Reply