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
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
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)
| Term | What it is | Purpose |
|---|---|---|
final | Keyword | Lock variable / method / class |
finally | Block with try-catch | Cleanup 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");
}
Final तीन levels पर
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 नहीं
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 |
|---|---|---|
final | Keyword | Variable / method / class lock करना |
finally | try-catch के साथ block | Cleanup 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 करें");
}
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 है.