Member-only story
Difference Between final, finally and finalize() in Java
🔒 This is a Medium member-only article. If you’re not a Medium member, you can read the full article for free on my blog: Difference Between final, finally and finalize() in Java.
In Java, the terms final
, finally
, and finalize()
may look and sound similar, but they serve completely different purposes. While beginners often confuse them, each has its unique use case — one deals with immutability, another with exception handling, and the last one with garbage collection.
In this article, we will dive deep into each of these keywords/methods, understand what they do, how they work, and explore real examples to avoid confusion.
1. What is final
in Java?
final
is a keyword in Java that is used to restrict the behavior of variables, methods, and classes. Once something is marked as final
, it cannot be changed or modified.
âś… Use Cases of final
:
a. Final Variables:
A variable declared as final
cannot be reassigned once initialized.
final int x = 100;
x = 200; // ❌ Compile-time error
This is useful when you want to create constants.
b. Final Methods:
A method marked as final
cannot be overridden by subclasses.
class Parent {
final void show() {
System.out.println("Parent…