Difference Between final, finally, and finalize() in Java
In Java, the terms final, finally, and finalize() often confuse beginners due to their similar names. However, each has a very different purpose and use case. Let’s break them down in detail.
1. final Keyword
1.1 final Variable
final int MAX_AGE = 100;
// MAX_AGE = 90; // Compilation error
1.2 final Method
class Animal {
final void sound() {
System.out.println("Animal sound");
}
}
class Dog extends Animal {
// void sound() {} // Error
}
1.3 final Class
final class Constants {
public static final double PI = 3.14159;
}
// class ExtendedConstants extends Constants {} // Error
2. finally Block
public class TryFinallyExample {
public static void main(String[] args) {
try {
int data = 100 / 5;
System.out.println(data);
} catch (Exception e) {
System.out.println("Exception caught: " + e);
} finally {
System.out.println("finally block executed");
}
}
}
Output
20
finally block executed
3. finalize() Method
protected void finalize() throws Throwable {
// cleanup code
super.finalize();
}
Example
public class FinalizeExample {
protected void finalize() {
System.out.println("Finalize method called");
}
public static void main(String[] args) {
FinalizeExample obj = new FinalizeExample();
obj = null;
System.gc(); // Suggests GC
}
}
Summary Table
| Feature | final | finally | finalize() |
|---|---|---|---|
| Type | Keyword | Block | Method |
| Usage | Restrict modification | Exception handling cleanup | Garbage collection cleanup |
| Called by | Programmer | JVM during try-catch | JVM during GC |
| Modern Use | Recommended | Recommended | Deprecated |
Conclusion
Understanding the difference between final, finally, and finalize() helps you write more secure and efficient Java code. Use them wisely according to their purpose.
๐งน Master Java Keywords & Resource Cleanup
Understanding the difference between final, finally, and finalize() is essential for writing safe, clean, and interview-ready Java code. Explore these related Java fundamentals to strengthen your understanding of exception handling, memory management, and real-world coding practices.
๐จ Java Exception Handling Best Practices
Learn how finally blocks are used correctly in real-world applications.
❓ Exception Handling Interview Questions
Common interview questions involving finally and cleanup logic.
๐ง Java Memory Leaks & Prevention
Understand why relying on finalize() can cause memory leaks.
⚙️ Java Multithreading Interview Questions
Learn how finalize() and cleanup behave unpredictably in multithreaded code.
๐งต Java String Pool & Interning
Reinforce the concept of immutability using the final keyword.
๐ฆ Java Collections Interview Questions
Learn how final references behave with mutable collections.
๐ง Java Coding Round Questions
Practice interview-style problems involving final and exception handling.
๐งช Advanced Java Programs (Real-World)
Apply best practices in production-style Java programs.
๐ Java 21 Interview Questions
Modern Java interview discussions around immutability and cleanup.
๐ Java 25 Interview Questions & Answers
Learn how finalize() deprecation is discussed in modern interviews.