In Java, final, finally, and finalize() are three different concepts that serve different purposes. The final keyword is used to restrict changes, finally is used in exception handling, and finalize() is related to garbage collection.
- final prevents modification of variables, methods, and classes.
- finally always executes after the try-catch block.
- finalize() runs before an object is garbage collected (deprecated in Java 9+).
final Keyword
The final keyword is used to restrict changes in Java. It can be applied to variables, methods, and classes. Once declared as final, certain modifications are not allowed.
- A final variable cannot be reassigned after initialization.
- A final method cannot be overridden by subclasses.
- A final class cannot be inherited.
Syntax
// Constant value
final int b = 100;
Example: The below Java program demonstrates the value of the variable cannot be changed once initialized.
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 block is used in exception handling. It contains code that is executed after the try and catch blocks, regardless of whether an exception occurs or not.
- Always executes after the try-catch block.
- Used for cleanup tasks such as closing files or database connections.
- Executes even if an exception is thrown.
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.
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 defined in the Object class and is called by the Garbage Collector before an object is removed from memory. It was traditionally used for cleanup operations.
- Invoked before an object is garbage collected.
- Defined in the Object class.
- Used for resource cleanup before object destruction.
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.
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().
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. | A finally block can also be used with try without catch. | 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. |