Member-only story

Demystifying the Differences: final, finally, and finalize in Java

Naveen Metta
4 min readJun 27, 2023

--

Introduction:

As Java programmers, we encounter various keywords that play vital roles in our code. Among them are final, finally, and finalize, which may sound similar but have distinct meanings and use cases. In this article, we will delve into the world of final, finally, and finalize in Java, unraveling their nuances, practical applications, and best practices. Whether you’re a seasoned developer or a beginner, understanding these keywords is crucial for writing robust and efficient Java programs.

The Mighty final Keyword:
The final keyword in Java is used to define entities that cannot be modified. Let’s explore its different applications in detail.

Final Variables:
When a variable is declared as final, its value cannot be changed once initialized. This provides immutability, ensuring that the variable’s value remains constant throughout its scope. Final variables are commonly used for constants or shared variables in multi-threaded environments. Consider the following example:

final int MAX_VALUE = 100;
// MAX_VALUE = 200; // Compilation error: cannot assign a value to a final variable

Final Methods:
When a method is declared as final, it cannot be overridden by subclasses. This can be advantageous when we want to ensure that the behavior of a method remains consistent across all subclasses. By making a method final, we enforce its…

--

--

Naveen Metta
Naveen Metta

Written by Naveen Metta

I'm a Full Stack Developer with 3+ years of experience. feel free to reach out for any help : mettanaveen701@gmail.com

No responses yet