Difference between final, finally, and finalize()
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.
- Using final keyword with Class:
XYZ.java
final class ABC { /* Methods and variable can be declared here. */ } /*Compile time error. XYZ can’t inherit ABC as it is final. */ public class XYZ extends ABC { /* Driver Code */ public static void main(String args[]) { } }
Output:

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
public class Sample { /* final method */ final void test() { System.out.println("Inside test method"); } /* Driver code */ public static void main(String a[]) { Sample s=new Sample(); s.test(); } } class PQR extends Sample { /* Compile time error. Can't override test() because it is final. */ void test() {} }
Output:

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
public class Sample { /* Driver Code */ public static void main(String a[]) { // Simple variable int p = 5; // Variable with final keyword final int q = 6; /* modifying the simplevariable : Allowed */ p++; /* Compile Time error. Can’t modify final variable*/ q++; } }
Output:

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
public class FinallyExample { /* methodOne()with try-catch and finally block. Called inside try-catch block in driver code */ static void methodOne() { try { System.out.println("Inside First try block"); throw new RuntimeException("e"); } finally { System.out.println("Inside first finally"); } } /*Method called outside try-catch block in driver code.*/ static void methodOne() { try { System.out.println("Inside Second try block"); return; } finally { System.out.println("Inside second finally"); } } /* Driver Code */ public static void main(String args[]) { try { methodOne(); } catch (Exception e) { System.out.println("Exception caught"); } methodTwo(); } }
Output:

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
public class FinalizeExample { @Override protected void finalize() { System.out.println("finalize method executed"); } /*Driver Code */ public static void main(String a[]) { Sample h1 = new Sample(); h1 = null; /* Marking h1 for garbage collection */ System.gc(); /*Garbage collector calls finalize */ System.out.println("Main completed"); } }
Output:
Main completed finalize method executed
The above code shows the use of finalize() method overridden inside FinalizeExample class.
Difference between final, finally, and finalize()
Sr. no. | final | finally | finalize() |
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.