Free tutorials in Hindi & English Daily computer, mobile and IT guides Beginner friendly learning
Blog · Java · 04 Jul 2026 · Hindi + English

Final Keyword in Java: Variable, Method and Class

final variable = constant, final method = cannot override, final class = cannot extend. Examples of all three plus final vs finally vs finalize.

final at three levels

final variable = value locked (constant). final method = cannot be overridden. final class = cannot be extended. One keyword, three protections.

1. final variable

final int MAX_MARKS = 100;
MAX_MARKS = 90;          // ERROR: cannot assign a value to final variable

final Student s = new Student("Aman");
s.marks = 95;            // OK!  object's fields can change
s = new Student("Priya"); // ERROR: the REFERENCE is locked, not the object
Interview trap: for objects, final locks the reference, not the object's contents. final List can still have items added — you just cannot point the variable to a different list. Exactly like int *const p in C.

2. final method

class Payment {
    final void verifyOTP() {                 // locked logic
        System.out.println("OTP verified securely");
    }
}

class UpiPayment extends Payment {
    // void verifyOTP() { ... }
    // ERROR: verifyOTP() in Payment is final, cannot be overridden
}

Use when a method's logic is security- or correctness-critical and no subclass should be able to change it.

3. final class

final class Constants {
    static final double GST = 0.18;
}

// class MyConstants extends Constants { }
// ERROR: cannot inherit from final class Constants

Famous final classes in Java itself: String, Integer, Math. String is final so nobody can subclass it and break its immutability guarantee — a favourite follow-up question.

final vs finally vs finalize (the trio question)

TermWhat it isPurpose
finalKeywordLock variable / method / class
finallyBlock with try-catchCleanup code that always runs, exception or not
finalize()Method (deprecated)Was called by GC before destroying object; deprecated since Java 9
try {
    int x = 10 / 0;
} catch (ArithmeticException e) {
    System.out.println("Error caught");
} finally {
    System.out.println("Always runs - close files here");
}
Error caught Always runs - close files here

Final तीन levels पर

final variable = value locked (constant). final method = override नहीं हो सकता. final class = extend नहीं हो सकती. एक keyword, तीन protections.

1. final variable

final int MAX_MARKS = 100;
MAX_MARKS = 90;          // ERROR: cannot assign a value to final variable

final Student s = new Student("Aman");
s.marks = 95;            // OK!  object की fields बदल सकती हैं
s = new Student("Priya"); // ERROR: REFERENCE locked है, object नहीं
Interview trap: objects के लिए final reference lock करता है, object का content नहीं. final List में items add हो सकते हैं — बस variable को दूसरी list पर point नहीं कर सकते. बिल्कुल C के int *const p जैसा.

2. final method

class Payment {
    final void verifyOTP() {                 // locked logic
        System.out.println("OTP verified securely");
    }
}

class UpiPayment extends Payment {
    // void verifyOTP() { ... }
    // ERROR: verifyOTP() in Payment is final, cannot be overridden
}

तब use करें जब method की logic security- या correctness-critical हो और कोई subclass उसे बदल न पाए.

3. final class

final class Constants {
    static final double GST = 0.18;
}

// class MyConstants extends Constants { }
// ERROR: cannot inherit from final class Constants

Java की famous final classes: String, Integer, Math. String final है ताकि कोई subclass बनाकर उसकी immutability guarantee न तोड़ सके — favourite follow-up question.

final vs finally vs finalize (trio question)

Termक्या हैPurpose
finalKeywordVariable / method / class lock करना
finallytry-catch के साथ blockCleanup code जो हमेशा चलता है, exception हो या न हो
finalize()Method (deprecated)GC object destroy करने से पहले call करता था; Java 9 से deprecated
try {
    int x = 10 / 0;
} catch (ArithmeticException e) {
    System.out.println("Error caught");
} finally {
    System.out.println("Always runs - files यहां close करें");
}
Error caught Always runs - files यहां close करें

Frequently Asked Questions

What does the final keyword do in Java?

On a variable it locks the value (or reference), on a method it prevents overriding, and on a class it prevents inheritance.

Can a final object be modified?

Its fields can change — final only locks the reference, so the variable cannot point to another object, but the object itself remains mutable.

What is the difference between final, finally and finalize?

final is a keyword for locking; finally is a try-catch block that always executes for cleanup; finalize() was a garbage-collection callback method, deprecated since Java 9.