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.
Factor | final | finally | finalize |
---|---|---|---|
Definition | final is a keyword which is used as access modifier in Java | finally is a block in Java used for Exception Handling | finalize() is a method in Java used for Garbage Collection |
Usage | final 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. |
Execution | It is executed when it is invoked by the JVM | Executes right after the execution of try-catch block | It 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
package com.vidvaan.corejava.exception11.FinalVsFinallyVsFinalize; // final variable demo public class FinalVariableDemo { // final instance variable declaration and assignment private final int finalInstanceVariable = 11; // final instance variable declaration and can be assigned in constructor private final int finalInstanceVariableDeclaration; // non-final instance variable can be re-assigned anywhere private int nonFinalInstanceVariable = 22; public FinalVariableDemo() { // finalInstanceVariable = 33; // Compilation Error : The final field FinalVariableDemo.finalInstanceVariable // cannot be assigned this.finalInstanceVariableDeclaration = 44; // Ok : declared final instance variable can be assigned in constructor // this.finalInstanceVariableDeclaration = 55; // Compilation Error : The final field finalInstanceVariableDeclaration may // already have been assigned this.nonFinalInstanceVariable = 66; // Ok : non-final variable can be assigned any where } public void changeFinalInstanceVariableValue() { // this.finalInstanceVariable = 77; // Compilation error : The final field FinalVariableDemo.finalInstanceVariable // cannot be assigned // this.finalInstanceVariableDeclaration = 88; // Ok : declared final instance variable can only assigned in constructor, but // not anywhere else this.nonFinalInstanceVariable = 99; // Ok : non-final variable can be assigned any where } public void changeFinalMethodLocalVariableValue() { // final local variable declaration and assignment final int finalLocalVariable = 111; final int finalLocalVariableDeclaration; int nonFinalLocalVariable = 222; // finalLocalVariable = 333; // Compilation error :The final local variable finalLocalVariable cannot be // assigned. It must be blank and not using a compound assignment finalLocalVariableDeclaration = 444; // Ok : declared final local variable can be assigned in constructor // finalLocalVariableDeclaration = 555; // Compilation Error: The final local variable finalLocalVariableDeclaration may // already have been assigned nonFinalLocalVariable = 666; // Ok : non-final variable can be assigned any where } public void changeFinalMethodParameterValue(final int finalMethodParameter, int nonFinalMethodParameter) { // finalMethodParameter = 1111; // Compilation error: The final local variable finalMethodParameter cannot be // assigned. It must be blank and not using a compound assignment nonFinalMethodParameter = 2222; // OK: non-final variable can be assigned any where } } |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
package com.vidvaan.corejava.exception11.FinalVsFinallyVsFinalize; public class FinalMethodChild extends FinalMethodParent { @Override public void nonFinalMethod() { // OK } // @Override // public void finalMethod() { // Compilation error // } } class FinalMethodParent { public void nonFinalMethod() { } public final void finalMethod() { } } |
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
1 2 3 4 5 6 7 8 9 10 |
package com.vidvaan.corejava.exception11.FinalVsFinallyVsFinalize; public final class FinalParentClass { } //final class FinalChildClass extends FinalParentClass{ //Compilation error //} |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
package com.vidvaan.corejava.exception11.FinalVsFinallyVsFinalize; // final class methods by default final, but variable are not final class FinalClassMembers { // by default it is final. public void method() { } // by default it is not final. static int staticVariable = 30; // by default it is not final. int nonStaticVariable = 5; public static void main(String[] args) { // See modified contents of variable j. FinalClassMembers obj = new FinalClassMembers(); obj.nonStaticVariable = 6; // We can change the value because it is not final staticVariable = 36; // We can change the value because it is not final } } |
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.
1 |
protected void finalize() throws Throwable { } |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package com.vidvaan.corejava.exception11.FinalVsFinallyVsFinalize; // Garbage Collector - invokes finalize() method public class Example01FinalizeMethodCalledByGC { // This method executed by Garbage Collector just before this object Garbage // Collected @Override protected void finalize() throws Throwable { System.out.println("finalize() method"); } public static void main(String[] args) throws Exception { Example01FinalizeMethodCalledByGC object = new Example01FinalizeMethodCalledByGC(); object = null; System.gc(); // Requesting JVM to invoke Garbage Collector Thread.sleep(1000); // To make sure Garbage Collector gets chance from thread scheduler } } |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
package com.vidvaan.corejava.exception11.FinalVsFinallyVsFinalize; //Explicitly invoke finalize() method public class Example02ExplicitlyCallFinalizeMethod { @Override protected void finalize() throws Throwable { System.out.println("finalize() method"); } public static void main(String[] args) throws Throwable { Example02ExplicitlyCallFinalizeMethod object = new Example02ExplicitlyCallFinalizeMethod(); System.out.println("main begin"); // Calling finalize method Explicitly. object.finalize(); // To make sure object is not destroyed after explicit call of finalize() method object.toString(); // If this lines executes it is the proof that object is not destroyed object = null; // Requesting JVM to invoke Garbage Collector System.gc(); System.out.println("main end"); } } |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
package com.vidvaan.corejava.exception11.FinalVsFinallyVsFinalize; // finalize() method explicit call - exception raised in finalize() method execution public class Example03ExplicitlyCallFinalizeUncheckedExceptionRaised { @Override protected void finalize() throws Throwable { System.out.println("finalize() method begin"); int result = 5 / 0; // It will throw unchecked ArithmeticException, program gets terminated System.out.println("finalize() method end"); } public static void main(String[] args) throws Throwable { Example03ExplicitlyCallFinalizeUncheckedExceptionRaised object = new Example03ExplicitlyCallFinalizeUncheckedExceptionRaised(); System.out.println("main begin"); // Calling finalize() method Explicitly. object.finalize(); System.out.println("main end"); } } |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
package com.vidvaan.corejava.exception11.FinalVsFinallyVsFinalize; // finalize() method called by Garbage collector - exception raised in finalize() execution public class Example04GCCallFinalizeMethodUncheckedRaised { @Override protected void finalize() throws Throwable { System.out.println("finalize() method begin"); int result = 5 / 0; // It will throw unchecked ArithmeticException, remaining lines ignored System.out.println("finalize() method end"); } public static void main(String[] args) throws Throwable { Example04GCCallFinalizeMethodUncheckedRaised object = new Example04GCCallFinalizeMethodUncheckedRaised(); System.out.println("main begin"); object = null; // Requesting JVM to invoke Garbage Collector System.gc(); System.out.println("main end"); } } |
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.