Difference Between final, finally, and finalize() in Java

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.