Java final, finally and finalize
In Java, the final, finally, and finalize keywords play an important role in exception handling. The main difference between final, finally, and finalize is listed below:
- final: The final is the keyword that can be used for immutability and restrictions in variables, methods, and classes.
- finally: The finally block is used in exception handling to ensure that a certain piece of code is always executed whether an exception occurs or not.
- finalize: finalize is a method of the object class, used for cleanup before garbage collection.
final vs finally vs finalize
The table below demonstrates the difference between final, finally, and finalize.
final | finally | finalize |
---|---|---|
final keyword applies restrictions on variable, method and classes. | The finally block in exception handling is used with try-catch block. | finalize is a method of object class |
Prevent modification of variables, inheritance of classes, or overriding of methods. | The code that is written inside finally block is always executed after the try-catch block whether an exception occurs or not . | finalize method in Java is used to perform cleanup operations before an object is garbage collected. |
Variables, methods, and classes. | Only within a | Objects, specifically by overriding the method in a class |
Executes when declared. | Always executed after try-catch block. | Called by the garbage collector when an object is about to be deleted, but it's not guaranteed to run. |
Now, we will discuss each keyword in detail one by one.
final Keyword
The final keyword in Java is used with variables, methods, and also with classes to restrict modification.
Syntax:
// Constant value
final int a = 100;
Example: The below Java program demonstrates the value of the variable cannot be changed once initialized.
// Java Program to demonstrate
// the final keyword with variable
class A {
public static void main(String[] args)
{
// Non final variable
int a = 5;
// final variable
final int b = 6;
// modifying the non final variable
a++;
// modifying the final variable
// Immediately gives Compile Time error
b++;
}
}
Output:

Note: If we declare any variable as final, we can't modify its contents since it is final, and if we modify it then we get Compile Time Error.
In the above example, we used the final
keyword to declare a variable that cannot be modified after its initialization. Similarly, the final
keyword can also be applied to methods and classes in Java to impose certain restrictions.
finally Keyword
The finally
keyword in Java is used to create a block of code that always executes after the try
block, regardless of whether an exception occurs or not.
Syntax:
try {
// Code that might throw an exception
} catch (ExceptionType e) {
// Code to handle the exception
} finally {
// Code that will always execute
}
Example: The below Java program demonstrates the working of finally block in exception handling.
// Java program to demonstrate
// the working of finally block
public class Geeks {
public static void main(String[] args)
{
try {
System.out.println("Inside try block");
int result
= 10 / 0; // This will cause an exception
}
catch (ArithmeticException e) {
System.out.println("Exception caught: "
+ e.getMessage());
}
finally {
System.out.println(
"finally block always execute");
}
}
}
Output:

Explanation: The try block attempts a division by zero, causing an ArithmeticException. The finally block executes, whether an exception occurs, ensuring cleanup or mandatory code execution.
finalize() Method
The finalize() method is called by the Garbage Collector just before an object is removed from memory. It allows us to perform clean up activity. Once the finalized method completes, Garbage Collector destroys that object.finalize method is present in the Object class.
Syntax:
protected void finalize throws Throwable{}
Note: finalize() is deprecated in Java 9 and should not be used in modern applications. It’s better to use try-with-resources or other cleanup mechanisms instead of relying on finalize().
Example: The below Java program demonstrates the working of finalize() method in context of garbage collection.
// Java Program to demonstrates
// the working of finalize() Method
import java.util.*;
public class Geeks {
public static void main(String[] args)
{
Geeks g = new Geeks();
System.out.println("Hashcode is: " + g.hashCode());
// Making the object eligible for garbage collection
g = null;
System.gc();
// Adding a short delay to allow GC to act
try {
Thread.sleep(1000);
}
catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("End of the garbage collection");
}
// Defining the finalize method
@Override protected void finalize()
{
System.out.println("Called the finalize() method");
}
}
Output:

Explanation: In the above example, an object "g" is created, and its hash code is printed. The object is made eligible for garbage collection by setting it to null and invoking System.gc().