Java final vs finally vs finalize

In Java, the words final, finally, and finalize are quite different from each other. You can see the following table to understand Definition, Usage and Execution of final, finally and finalize.

Factorfinalfinallyfinalize
Definitionfinal is a keyword which is used as access modifier in Javafinally is a block in Java used for Exception Handlingfinalize() is a method in Java used for Garbage Collection
Usagefinal modifiers is applicable for classes, methods and variables.

 If a class declared as final then the child class creation is not possible.

 If a method declared as final then overriding of that is not possible.

 If a variable declared as final then re-assignment is not possible.
It is the block always associated with try /catch to maintain clean up code which should be executed always irrespective of whether exception raised or not raised and whether handled or not handled(if exception is raised).It is a method which should be called by garbage collector. Just before destroying an object to perform clean-up activities.
ExecutionIt is executed when it is invoked by the JVMExecutes right after the execution of try-catch blockIt executes just before an object is destroyed

final keyword

final is a keyword in java. We can’t use it as an identifier as it is reserved. We can use this keyword with variables, methods and also with classes.

  • If a class declared as final then the child class creation is not possible.
  • If a method declared as final then overriding of that is not possible.
  • If a variable declared as final then re-assignment is not possible.

Example 01 : final variable – we can’t change variable value

In the above example, As we can see the compiler forbids us to assign a new value to final instance variable, final method parameter, final local variable.

Example 02 : final method – we can’t change method definition means we can’t override final method

In the above example, As we can see the compiler forbids us to redefine final method in the child class

Example 03 : final class – we can’t change class definition means we can’t inherit final class

In the above example, As we can see the compiler forbids us to define child class for final parent class.

Example 04 : final class members

In the above example, As we can see If a class is declared as final then by default all of the methods are final but variables are not final.

finally block

View the Java finally block page for finally block detailed explanation.

finalize() Method

The finalize() method is a special method which is called by the garbage collector on an object which is eligible for Garbage Collection. Object is eligible for garbage collection when there are no more references to the object(unreferenced object). Once the finalize() method completes, immediately Garbage Collector destroy that object. finalize() method is defined in java.lang.Object class and its syntax is as follows.

finalize() method is used to perform clean-up activity. Clean-up activity could be resource releasing(DB Connection, Network Connection, File, stream … etc.).

Since Object class contains the finalize() method it inherits into every java object. So Garbage Collector calls finalize() method on all java objects.

The finalize method in the Object class, has an empty implementation. If we have clean-up activities in our class then we have to override this method to define our own clean-up activities.

Example 01 : finalize() method called by Garbage Collector

Output :

finalize() method

In the above example, we are calling System.gc(); for requesting JVM to invoke Garbage Collector. But there is no guarantee that always garbage collector executes with System.gc().

Example 02 : Explicitly invoke finalize() method

Output :

main begin
finalize() method
main end
finalize() method

In the above example, As we are invoking finalize() method explicitly, object won’t get destroyed. To prove that, after calling finalize() method explicitly we are calling toString(); method, it executed without fail. It proves that, when we invoke finalize() method explicitly, that method will be executed like a normal method.

Example 03 : Explicitly invoke finalize() method – exception raised in finalize() method execution

Output :

main begin
finalize() method begin
Exception in thread “main” java.lang.ArithmeticException: / by zero
at com.vidvaan.corejava.exception11.FinalVsFinallyVsFinalize.Example03ExplicitlyCallFinalizeUncheckedExceptionRaised.finalize(Example03ExplicitlyCallFinalizeUncheckedExceptionRaised.java:9)
at com.vidvaan.corejava.exception11.FinalVsFinallyVsFinalize.Example03ExplicitlyCallFinalizeUncheckedExceptionRaised.main(Example03ExplicitlyCallFinalizeUncheckedExceptionRaised.java:20)

In the above example, We are calling finalize() method explicitly and we can see that program terminated as there is exception raised in finalize() method execution.

Exception could be checked or unchecked program gets terminated when we invoke finalize() method explicitly.

Example 04 : finalize() method called by Garbage collector – exception raised in finalize() execution

Output :

main begin
main end
finalize() method begin

In the above example, Garbage Collector calling finalize() method. We can see that program not terminated even exception raised in finalize() method execution. But note that, after exception raised remaining lines of finalize() method are ignored and even exception is not displayed in the console.

Exception could be checked or unchecked remaining lines of finalize() method are ignored and exception details will not be printed on the console when finalize() method invoked by Garbage Collector.

Scroll to Top