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 लगाते हैं, तो उसे भविष्य में बदला नहीं जा सकता।

There are three main uses of the final keyword:
final कीवर्ड का उपयोग मुख्य रूप से तीन स्थानों पर किया जाता है:


📌 a) Final Variable

When a variable is declared with the 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 ब्लॉक में इनिशियलाइज़ किया जा सकता है।

Syntax:
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 / कंपाइल समय त्रुटि
}
}

The only difference between a final variable and a normal variable is that a final variable cannot be reassigned.
एक final वेरिएबल और सामान्य वेरिएबल में अंतर बस इतना है कि final वेरिएबल को दोबारा मान नहीं दिया जा सकता।


📌 b) Final Method

A method declared with the 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

A class declared as 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

Popular posts from this blog

What is a Web Browser? वेब ब्राउज़र क्या है?

Java's Support System जावा का सहयोगी तंत्र

The Internet and Java इंटरनेट और जावा का सम्बन्ध