Difference between final, finally, and finalize()

In Java, final, finally and finalize sound similar they are totally different in functionality. Final and finally are the two keywords but the finalize is a method of the Object class. In this section, we will discuss the differences between final, finally, and finalize().

Java final keyword

final is a keyword in Java that means we cannot use final as an identifier in the Java program. When a class, method and a variable name is preceded with final keyword it means that they cannot be inherited by another class, the method cannot be overridden by another method, and the value assigned to the variable cannot be updated respectively.

Let’s use the final keyword with class, method, and variable in Java programs.

  1. Using final keyword with Class:

XYZ.java

          Output:

Difference between final, finally, and finalize()

The above code shows the use of final keyword with class ABC. Class XYZ cannot inherit class ABC as it is a final class.

  • Using final Keyword with Method

Sample.java

Output:

Difference between final, finally, and finalize()

The above code shows the use of final keyword with test() method declared inside Sample class. The method cannot be overridden in the child class PQR.

3. Using final Keyword with Variable:

Sample.java

Output:

Difference between final, finally, and finalize()

The above code shows the use of final keyword with variable qdeclared inside Sample class.

Java finally keyword

In Java, finally is a reserved keyword. It is used in exception handling. The finally block is always used after try-catch block.The finally block executes even if an unexpected exception occurs.Writing the cleanup code in a finally block is always a good practice, even when no exceptions are predicted.

FinallyExample.java

Output:

Difference between final, finally, and finalize()

The above code shows the use of finally block declared inside methodOne() and methodTwo() in the FinallyExampleclass.

Java finalize() method

In Java, the finalize() method is called by the garbage collector in order to free up the memory heap. It is called just before removing the unused objects. It is a method of the Object class. It is called when an object is dereferenced. The dereferenced object needs to be destroyed by the garbage collector.

FinalizeExample.java

Output:

The above code shows the use of finalize() method overridden inside FinalizeExample class.

Difference between final, finally, and finalize()

Sr. no.finalfinallyfinalize()
1.final is a keyword/access modifier.finally is a block used with try-catch block.finalize() is a protected method declared in java.lang.Object class.
2.Classes, variables, and methods are preceded by the final keyword.The try-catch block is followed by finally block. It is not necessary to use finally. But it is recommended to use finally block while handling an exception.finalize() method works on objects that are marked for garbage collection.
3.Using the final keyword implies restrictions on class, method, and variables. The class cannot be extended, the method cannot be overridden, variable value cannot be updated.finally block is used during exception handling. Even if try-catch block doesn’t execute, statements inside finally block will be executed.While using finalize() method, it should be overridden. It is used for garbage collection.
4.final method is executed when the programmer makes a call to it.finally block performs it’s execution after the try-catch block. finalize() method executes before garbage collector destroys the dereferenced objects.

In this article we have discussed final, finally and finalize() with examples. The key differences among themare also explained.

Pin It on Pinterest

Share This