Showing posts with label java variable scope. Show all posts
Showing posts with label java variable scope. Show all posts

Sunday, November 26, 2023

Scope of a variable in java जावा में वेरिएबल का कार्यक्षेत्र

In Java, the scope of a variable refers to the region in a program where the variable is recognized and accessible. Variables in Java can be categorized based on their scope into three main types:

जावा में वेरिएबल का कार्यक्षेत्र उस हिस्से को कहा जाता है जहाँ वेरिएबल मान्य और उपयोग योग्य होता है। जावा में वेरिएबल के कार्यक्षेत्र को तीन प्रमुख वर्गों में बांटा गया है–


1️⃣ Local Variable (लोकल वेरिएबल)

English:

  • Declared and used only inside a method or a block {}.

  • Scope is limited to that method or block.

  • Dies as soon as the control exits the method.

  • Java does not allow same-name variables in nested blocks unlike C/C++.

Hindi:

  • यह किसी मेथड या ब्लॉक {} के अंदर ही घोषित और उपयोग किए जाते हैं।

  • इनका कार्यक्षेत्र उसी मेथड या ब्लॉक तक सीमित होता है।

  • जैसे ही कंट्रोल बाहर जाता है, यह समाप्त हो जाते हैं।

  • जावा में नेस्टेड ब्लॉक्स में एक ही नाम के दो वेरिएबल नहीं बनाए जा सकते हैं।


2️⃣ Instance Variable (इंस्टैंस वेरिएबल)

English:

  • Declared inside a class but outside methods.

  • Every object has its own copy of instance variables.

  • Lives as long as the object lives.

Hindi:

  • यह क्लास के अंदर लेकिन मेथड के बाहर घोषित किए जाते हैं।

  • क्लास के हर ऑब्जेक्ट की अपनी एक कॉपी होती है।

  • जब तक ऑब्जेक्ट मौजूद रहता है, यह भी रहते हैं।


3️⃣ Class Variable (क्लास वेरिएबल)

English:

  • Declared with static keyword inside the class.

  • Shared among all objects of the class.

  • Memory is allocated only once.

Hindi:

  • इसे static कीवर्ड से क्लास के अंदर घोषित किया जाता है।

  • यह क्लास के सभी ऑब्जेक्ट्स के बीच साझा होता है।

  • मेमोरी में केवल एक बार ही इसका स्थान निर्धारित होता है।


🔎 Example Program (उदाहरण प्रोग्राम)

public class VariableScopeExample {

    // Class variable
    static int globalVar = 10;

    public static void main(String[] args) {
        // Local variable
        int localVar = 5;

        System.out.println("Local Variable: " + localVar);
        System.out.println("Class Variable: " + globalVar);

        // Creating an object of SampleClass
        SampleClass obj = new SampleClass();

        // Accessing instance variable through object
        System.out.println("Instance Variable through Object: " + obj.instanceVar);
    }
}

class SampleClass {
    // Instance variable
    int instanceVar = 20;
}

📌 Summary सारांश:

  • Local variables are short-lived and exist only in blocks or methods.

  • Instance variables are object-specific and non-static.

  • Class variables are shared and declared with static.

Variables in Java जावा में वेरिएबल

In Java, a variable is a named storage location used to hold data values during program execution. Variables are essential because they allow you to store, update, and use data dynamically.

जावा में वेरिएबल (चर) एक नामित स्टोरेज स्थान होता है, जिसका उपयोग प्रोग्राम के दौरान डेटा को संग्रहित और उपयोग करने के लिए किया जाता है। वेरिएबल्स प्रोग्रामिंग का एक मूलभूत हिस्सा होते हैं।


🔹 1. Declaration | घोषणा

Before using a variable, it must be declared with its data type and name.

किसी वेरिएबल का उपयोग करने से पहले, उसका डेटा प्रकार और नाम बताकर उसे घोषित (declare) करना होता है।

int age; // Declare an integer variable named 'age'

🔹 2. Initialization | मान देना

After declaring, you can assign a value. This is called initialization.

घोषणा के बाद, वेरिएबल को कोई मान (value) देना Initialization कहलाता है।

age = 50; // Initialize the variable

Declaration and initialization can also be done together:

int age = 50;

🔹 3. Variable Types | वेरिएबल के प्रकार

Java is statically typed, so you must declare the type. Common types are:

int count = 10;         // Integer
double pi = 3.14;       // Decimal number
char grade = 'A';       // Character
boolean isStudent = true; // True or False

जावा में वेरिएबल का डेटा प्रकार पहले से तय करना होता है जैसे int, double, char, boolean आदि।


🔹 4. Naming Rules | नामकरण के नियम

  • Must start with a letter, _ or $

  • Can include letters, digits, _, and $

  • Case-sensitive (Age and age are different)

  • Reserved words like class, int cannot be used

वेरिएबल नाम छोटे अक्षरों से शुरू होते हैं और विशेष शब्दों (keywords) का उपयोग नहीं किया जा सकता।


🔹 5. Scope of Variable | वेरिएबल का स्कोप

Scope means where the variable can be accessed:

public class Example {
    int globalVar = 5; // Class-level (field)

    public void exampleMethod() {
        int localVar = 10; // Local to method
        System.out.println(localVar);
    }
}

वेरिएबल का स्कोप यह तय करता है कि वह किस भाग में मान्य है – जैसे क्लास लेवल, मेथड लेवल या ब्लॉक लेवल।


🔹 6. Final Keyword | अंतिम (Final) वेरिएबल

To make a variable constant, use final:

final double PI = 3.14159;

final का प्रयोग वेरिएबल को स्थायी (unchangeable) बनाने के लिए होता है।


✅ Java Program: Simple Interest | जावा प्रोग्राम: साधारण ब्याज

public class SimpleInterest {
    public static void main(String args[]) {
        // Declare variables
        float p, r, t, si;

        // Initialize variables
        p = 1000;   // Principal
        r = 2;      // Rate
        t = 5;      // Time

        // Calculate SI
        si = (p * r * t) / 100;

        // Print result
        System.out.println("Simple Interest is= " + si);
    }
}

Output:

Simple Interest is= 100.0

📝 Summary (सारांश):

  • Variables store data

  • Must be declared with type

  • Can be initialized separately or together

  • Scope defines accessibility

  • Use final to make constants

वेरिएबल्स डेटा स्टोर करने के लिए आवश्यक हैं, इन्हें पहले घोषित करना होता है, और final से स्थिरांक बनाया जा सकता है।

📚 Other Stream Classes in Java जावा में अन्य स्ट्रीम क्लासेस

In Java, apart from basic byte and character streams, there are several specialized stream classes that provide advanced input-output operat...