Launch your tech mastery with us—your coding journey starts now!
Course Content
Array Handling
0/1
String Handling
0/1
Wrapper Classes
0/1
Collections in Java
0/1
Packages
0/1
File Handling
0/1
Multithreading
0/1
Java Networking
0/1
Core Java

🔹 final Keyword in Java – The “Last Stop” 🚦

In Java, the final keyword is like putting a “Do Not Change 🚫” tag on something.
It can be applied to variables, methods, and classes — each with its own purpose.

👉 Think of it like this:

  • final variable → “This value is permanent, no one can change it!”

  • final method → “This method is fixed, no child can override it!”

  • final class → “This class is sealed, no one can extend it!”

Generated image

✨ Uses of final Keyword

1️⃣ Final Variable (Constant Value)

Once you assign a value, it cannot be changed.

class Test {
    final int SPEED_LIMIT = 100;  // Constant

    void display() {
        // SPEED_LIMIT = 120; ❌ Error – can't change final variable
        System.out.println("Speed limit is " + SPEED_LIMIT);
    }
}

public class Main {
    public static void main(String[] args) {
        Test t = new Test();
        t.display();
    }
}

✅ Output:

Speed limit is 100

💡 Tip: Usually written in UPPERCASE to show it’s a constant.


2️⃣ Final Method (No Overriding Allowed)

A final method in the parent cannot be overridden by child classes.

class Animal {
    final void sound() {
        System.out.println("Animal makes a sound");
    }
}

class Dog extends Animal {
    // void sound() { ❌ Error – can't override final method }
}

✔ This is useful when you want to protect important methods from being modified.


3️⃣ Final Class (No Inheritance)

A final class cannot be extended.

final class Vehicle {
    void display() {
        System.out.println("This is a vehicle");
    }
}

// class Car extends Vehicle { ❌ Error – can't inherit final class }

👉 Example in real Java:

  • The String class is final, so no one can extend it.


🚀 Quick Recap

Usage Meaning
final variable Value cannot be changed (constant).
final method Method cannot be overridden by child classes.
final class Class cannot be extended (no inheritance).

🎯 Why Learn final?

✔ Ensures security & integrity of critical code.
✔ Prevents unwanted modifications.
✔ Useful for constants, libraries, and frameworks.

💡 Interview Tip:
👉 Can we declare a constructor as final?
❌ No, because constructors are never inherited or overridden.