Java static Keyword – Explained with Examples जावा का स्टेटिक कीवर्ड – उदाहरणों सहित विस्तृत जानकारी

The static keyword in Java is primarily used for memory management. It can be applied to variables, methods, blocks, and nested classes. A static member belongs to the class rather than an instance, so it is shared among all objects of the class.

जावा में स्टेटिक कीवर्ड का प्रयोग मुख्य रूप से मेमोरी प्रबंधन के लिए किया जाता है। इसे वेरिएबल, मेथड, ब्लॉक और नेस्टेड क्लास में प्रयोग किया जा सकता है। एक स्टेटिक सदस्य क्लास का होता है, न कि किसी विशेष ऑब्जेक्ट का, इसलिए यह सभी ऑब्जेक्ट्स के बीच साझा होता है।


🔹 Uses of static Keyword स्टेटिक कीवर्ड के अनुप्रयोग

The static keyword is used with:

  • Static Block

  • Static Variable

  • Static Method

  • Static Class (Nested only)

स्टेटिक कीवर्ड का उपयोग निम्न के साथ किया जाता है:

  • स्टेटिक ब्लॉक

  • स्टेटिक वेरिएबल

  • स्टेटिक मेथड

  • स्टेटिक क्लास (केवल नेस्टेड)


🔸 Static Block स्टेटिक ब्लॉक

A static block is used to initialize static variables before the main method is executed. It runs only once when the class is loaded.

Static block का उपयोग static variables को main method से पहले initialise करने के लिए किया जाता है। यह केवल एक बार चलता है, जब क्लास लोड होती है।

static {
// initialization code
System.out.println("Static block initialized");
}

🔸 Static Variable स्टेटिक वेरिएबल

A variable declared with static is shared by all objects of the class. Only one copy exists in memory.

static कीवर्ड से घोषित वेरिएबल क्लास के सभी ऑब्जेक्ट्स द्वारा साझा किया जाता है। इसकी केवल एक कॉपी मेमोरी में रहती है।

static int count = 0; // shared by all objects

🔸 Static Method स्टेटिक मेथड

A method declared as static can be called without creating an object. It can access only static data directly and call other static methods.

static मेथड को बिना ऑब्जेक्ट बनाए ही कॉल किया जा सकता है। यह केवल अन्य static डेटा को डायरेक्ट एक्सेस कर सकता है।

public static void display() {
System.out.println("Static method called");
}

🔰 Example of Static Block, Variable and Method स्टेटिक ब्लॉक, वेरिएबल और मेथड का उदाहरण

public class StaticExample {
static int a = 25;
static int b;
static {
System.out.println("Static block initialized");
b = a * 5;
}
public static void main(String[] args) {
System.out.println("Value of a = " + a);
System.out.println("Value of b = " + b);
}
}

Output:

Static block initialized
Value of a = 25
Value of b = 125

🧱 Static Nested Class स्टेटिक नेस्टेड क्लास

A static nested class is a class declared within another class with the static keyword. It doesn't require a reference to the outer class to be instantiated.

Static nested class, outer class के अंदर static रूप से घोषित की जाती है। इसे outer class के ऑब्जेक्ट की आवश्यकता नहीं होती।

public class Outer {
private static String msg = "MyCode";
static class Inner {
public void display() {
System.out.println(msg);
}
}
public static void main(String[] args) {
Outer.Inner obj = new Outer.Inner();
obj.display();
}
}

Output:

MyCode

🆚 Difference Between Static and Non-static स्टेटिक और नॉन-स्टेटिक में अंतर

Static Non-Static
Belongs to class                               Belongs to object
Shared among all objects Separate for each object
Can be accessed without object Needs object to access
Used for memory efficiency Uses more memory
स्टेटिक नॉन-स्टेटिक
क्लास से संबंधित होता है ऑब्जेक्ट से संबंधित होता है
सभी ऑब्जेक्ट्स में साझा हर ऑब्जेक्ट के लिए अलग
ऑब्जेक्ट के बिना एक्सेस कर सकते हैं ऑब्जेक्ट आवश्यक होता है
मेमोरी की बचत करता है अधिक मेमोरी का उपयोग करता है

Comments

Popular posts from this blog

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

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

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