Sitemap

Member-only story

Final keyword explained

3 min readJun 28, 2025

--

Press enter or click to view image in full size

What is Final Keyword in Java?

The final keyword in Java is a non-access modifier used to impose restrictions. Depending on where it is used, it prevents:

  • Reassignment (for variables),
  • Overriding (for methods),
  • Inheritance (for classes).

It is mainly used to make code more secure, consistent, and predictable.

📌 We declare final in the following:

  1. Final Variable
  2. Final Method
  3. Final Class

Final Variable

A variable marked as final cannot be re-assigned once it has been initialized. It effectively becomes a constant.

public class Main{
public static void main(String[] args){
double pi = 3.14;
System.out.println("Original pi value: " + pi);

// Reassigning pi
pi = 3.14159;
System.out.println("Modified pi value: " + pi);
}
}

The above code shows how a fixed variable can be accidentally changed by another developer who is unaware of it. Using the final keyword prevents this from happening.

public class Main{
public static void main(String[] args){
final double PI = 3.14;
System.out.println("Pi value: " + PI)…

--

--