Java final, finally and finalize
Last Updated :
05 Aug, 2025
In Java, the keywords "final", "finally" and "finalize" have distinct roles. final enforces immutability and prevents changes to variables, methods or classes. finally ensures a block of code runs after a try-catch, regardless of exceptions. finalize is a method used for cleanup before an object is garbage collected.
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 b = 100;
Example: The below Java program demonstrates the value of the variable cannot be changed once initialized.
Java
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 value. Attempting to do so results in a 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
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:
outputExplanation: 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 finalize() 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
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:
OutputExplanation: 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().
final vs finally vs finalize
The table below illustrates the differences 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 try-catch block. | 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. |
Explore
Java Basics
OOP & Interfaces
Collections
Exception Handling
Java Advanced
Practice Java