final, finally, finalize
तीन मिलते-जुलते शब्द
final, finally और finalize एक जैसे दिखते हैं पर पूरी तरह असंबंधित हैं। final keyword है, finally block है, और finalize method है। यह पसंदीदा interview सवाल है, तो हर एक को साफ़ जानना मदद करता है।
final — keyword
final int MAX = 100;
// MAX = 200; // ERROR: final variable बदल नहीं सकते
final class Utility { } // extend नहीं हो सकती
// final method override नहीं हो सकती
final बदलाव रोकता है। Final variable दोबारा assign नहीं हो सकता, final method override नहीं हो सकती, और final class extend नहीं हो सकती। यह चीज़ों को constant या fixed बनाने के बारे में है।
finally — block
try {
int x = 5 / 0;
} catch (ArithmeticException e) {
System.out.println("Caught error");
} finally {
System.out.println("Always runs");
}
Always runs
finally block try-catch के बाद चाहे कुछ भी हो चलता है — exception हुआ हो या नहीं, और return हो तब भी। यह files बंद करने जैसी cleanup को इस्तेमाल होता है।
finalize — method
// finalize() garbage collection से पहले call होती थी
// protected void finalize() { ... }
// DEPRECATED — आधुनिक Java में इस्तेमाल न करें
finalize() वो method थी जिसे garbage collector object नष्ट करने से पहले call करता था, आख़िरी-मिनट cleanup को। यह अविश्वसनीय थी और अब deprecated है। आधुनिक code इसके बजाय try-with-resources इस्तेमाल करता है।
आमने-सामने
| बात | final | finally | finalize |
|---|---|---|---|
| क्या है | Keyword | Block | Method |
| मक़सद | बदलाव रोकना | हमेशा-चलने cleanup | Pre-GC cleanup |
| किसके साथ | var/method/class | try-catch | (deprecated) |
| आज आम | हाँ | हाँ | नहीं |
असल में क्या इस्तेमाल होता है
- final — अक्सर इस्तेमाल, constants को और classes/methods lock करने को।
- finally — cleanup को इस्तेमाल, हालाँकि try-with-resources अक्सर इसकी जगह लेता है।
- finalize — इस्तेमाल न कीजिए; यह deprecated है।
तो असली code में आप final और finally नियमित इस्तेमाल करते हैं, और finalize पूरी तरह बचाते हैं।
आम गलतियाँ
- इन्हें संबंधित समझना। ये अक्षर साझा करते हैं, मक़सद नहीं।
- Final variable दोबारा assign करना। यह compile error देता है।
- finally के skip होने की उम्मीद। यह try में return के बाद भी चलता है।
- finalize इस्तेमाल करना। यह deprecated है — try-with-resources इस्तेमाल कीजिए।
सारांश
finalkeyword है जो बदलाव रोकता है (variable, method, class)।finallyblock है जो try-catch के बाद हमेशा चलता है।finalizepre-garbage-collection method थी, अब deprecated।- final और finally नियमित इस्तेमाल कीजिए; finalize बचिए।
- ये सिर्फ़ मिलते-जुलते दिखते हैं — इनके काम असंबंधित हैं।
आगे, errors संभालिए custom exceptions से, या memory cleanup सीखिए garbage collection में।
अक्सर पूछे जाने वाले प्रश्न (FAQ)
final, finally और finalize में क्या अंतर है?
final keyword है जो किसी चीज़ को अपरिवर्तनीय बनाता है। finally block है जो try-catch के बाद हमेशा चलता है। finalize garbage collection से पहले call होने वाली method थी (अब deprecated)।final keyword क्या करता है?
final किसी चीज़ को constant बनाता है: final variable दोबारा assign नहीं हो सकता, final method override नहीं हो सकती, और final class extend नहीं हो सकती। यह बदलाव रोकने के बारे में है।finally block किसलिए इस्तेमाल होता है?
finally block try-catch के बाद चलता है चाहे exception हुआ हो या नहीं। यह उस cleanup को इस्तेमाल होता है जो हमेशा चलनी चाहिए, जैसे file या database connection बंद करना।क्या finalize अब भी Java में इस्तेमाल होता है?
finalize() deprecated है और इस्तेमाल नहीं करना चाहिए। यह object garbage-collect होने से पहले चलने को था, पर अविश्वसनीय था। इसके बजाय try-with-resources या स्पष्ट cleanup इस्तेमाल कीजिए।क्या try में return हो तब भी finally चलता है?
finally block तब भी चलता है जब try या catch block में return हो, exception फेंके, या break करे। बिल्कुल इसीलिए यह पक्की cleanup को इस्तेमाल होता है।