final Keyword in Java जावा में फाइनल कीवर्ड
In Java, the final
keyword is a special non-access modifier used to declare constants. When applied to variables, methods, or classes, it makes them unchangeable — meaning you cannot modify their value, override them, or extend them, respectively.
जावा में final
एक विशेष प्रकार का नॉन-एक्सेस स्पेसिफायर होता है जिसका उपयोग स्थिरांक (constant) बनाने के लिए किया जाता है। यदि हम किसी वेरिएबल, मेथड या क्लास के साथ final
लगाते हैं, तो उसे भविष्य में बदला नहीं जा सकता।
final
keyword:final
कीवर्ड का उपयोग मुख्य रूप से तीन स्थानों पर किया जाता है:
📌 a) Final Variable
final
keyword, it becomes a constant. You must initialize it at the time of declaration. If not, it becomes a blank final, which can only be initialized in a constructor or a static block.final
कीवर्ड के साथ घोषित किया जाता है, तो वह एक स्थिरांक बन जाता है। इसे घोषित करते समय ही मान देना होता है, अन्यथा यह blank final कहलाता है जिसे केवल कंस्ट्रक्टर या static ब्लॉक में इनिशियलाइज़ किया जा सकता है।final datatype variable_name = value;
final datatype वेरिएबल_name = value;
Example / उदाहरण:
class StudyRoom {
final int strength = 50;void display() {strength = 60; // Error / त्रुटि}public static void main(String args[]) {StudyRoom obj = new StudyRoom();obj.display(); // Compile-time error / कंपाइल समय त्रुटि}}
📌 b) Final Method
final
keyword cannot be overridden in any subclass. It ensures the method's logic remains unchanged.final
कीवर्ड के साथ घोषित किसी मेथड को किसी subclass में override नहीं किया जा सकता है। यह मेथड की स्थिरता सुनिश्चित करता है।
Example / उदाहरण:
class ABC {
public final void fm() {System.out.print("This is ABC class");}}class XYZ extends ABC {public final void fm() {System.out.print("This is XYZ class"); // Compile-time error / त्रुटि}}
📌 c) Final Class
final
cannot be extended. This is useful when you want to restrict inheritance.final
घोषित किया जाता है, तो उसे extend नहीं किया जा सकता। यह इनहेरिटेंस को सीमित करने के लिए उपयोगी होता है।Example / उदाहरण:
final class BaseClass {
public void show() {System.out.println("Hello world!");}}class DerivedClass extends BaseClass { // Compile-time error / त्रुटिpublic void show() {System.out.println("How are you?");}}
Comments
Post a Comment