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

Java में Final Keyword: Variable, Method और Class

final variable = constant, final method = override नहीं हो सकता, final class = extend नहीं हो सकती. तीनों के examples और 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

Java में final keyword क्या करता है?

Variable पर value (या reference) lock करता है, method पर overriding रोकता है, और class पर inheritance रोकता है.

क्या final object modify हो सकता है?

उसकी fields बदल सकती हैं — final सिर्फ reference lock करता है, यानी variable दूसरे object को point नहीं कर सकता, लेकिन object खुद mutable रहता है.

final, finally और finalize में क्या अंतर है?

final lock करने वाला keyword है; finally try-catch का block है जो cleanup के लिए हमेशा चलता है; finalize() garbage-collection callback method था, जो Java 9 से deprecated है.