Static Members of a Class in OOP

Static members of a Class

“Static का अर्थ होता है, ‘स्थिर’ या ‘अपरिवर्तनीय’। जब किसी Class के अंदर किसी Variable या Method को static keyword के साथ declare किया जाता है, तो वह static member कहलाता है।”

Static member किसी भी object का हिस्सा नहीं होता, यह सीधे Class से जुड़ा होता है। इसका मतलब है कि memory में इसकी केवल एक ही copy रहती है, चाहे Class के कितने भी Objects बनाए जाएं। इसे आप एक ऐसा shared resource मान सकते हैं, जिसे Class के सभी Objects आपस में share करते हैं।

 

Types (प्रकार)

Static members दो प्रकार के हो सकते हैं :

  1. Static Variables (Static Data Members)
  2. Static Methods (Static Functions)

1. Static Variables (Static Data Members)

ये वे Variables हैं, जिन्हें static keyword के साथ declare किया जाता है। इनकी सबसे बड़ी विशेषता यह है, कि इनकी memory में केवल एक ही copy बनती है। इस एक copy को Class के सभी Objects share करते हैं।

Key Points :

  • एक बार memory allocate होती है, और वो class की lifetime तक रहती है।
  • Object बनाते समय ये variable recreate नहीं होता।
  • ये variables class-name के साथ access किए जा सकते हैं।

Syntax

class ClassName {
public:
         static DataType variableName; // Declaration
};

Example (C++)

#include<iostream>
using namespace std;

class Student {
public:
         string name;
         static int studentCount; // Static variable declaration

         Student(string n) {
             name = n;
             studentCount++; // Increment shared counter
}

        void display() {
           cout << “Name: ” << name << endl;
}
};

// Static variable initialization
int Student::studentCount = 0;

int main() {
          Student s1(“Rahul”);
          Student s2(“Anjali”);
          Student s3(“Priya”);

          cout << “Total Students: ” << Student::studentCount << endl; // Access via class name

         return 0;
}

Explanation :

  • सभी Student objects के लिए common है।
  • हर नया Student object बनते ही studentCount बढ़ जाता है।
  • इसे object create किये बिना class-name से access किया जा सकता है।

2. Static Methods (Static Functions)

“ये Methods वे होते हैं, जिन्हें static keyword के साथ declare किया जाता है। इनकी सबसे महत्वपूर्ण बात यह है, कि ये केवल static variables को ही access कर सकते हैं। ये किसी भी Instance variable को सीधे access नहीं कर सकते, क्योंकि ये किसी object से जुड़े नहीं होते।”

Static methods का उपयोग utility functions बनाने के लिए किया जाता है, जिन्हें किसी object की आवश्यकता नहीं होती।

Key Points :

  • Object के बिना call किए जा सकते हैं।
  • Non-static members को directly access नहीं कर सकते।
  • Utility functions के लिए perfect हैं।

Syntax

class ClassName {
public:
          static ReturnType methodName(Parameters) {
         // Code
}
};

Example

#include <iostream>
using namespace std;

class Calculator {
public:
          static int add(int a, int b) { // Static method
             return a + b;
}
};

int main() {
        int result = Calculator::add(5, 10); // Call without creating object
        cout << “Sum: ” << result << endl;
        return 0;
}

Explanation :

  • Calculator class में एक static method add() defined है।
  • add() दो numbers को add करके return करता है।
  • main() function में object create किए बिना इसे class-name से call किया गया |
  • Result store किया गया result variable में और cout से print किया गया।

Advantages of Static Members (लाभ)

  1. Memory Efficient – Single copy class level पर store होती है, हर object के लिए duplicate memory नहीं बनती।
  2. Shared Data – सभी objects एक ही data variable को share करते हैं।
  3. Object-Independent Methods – Static methods को object create किए बिना call किया जा सकता है।
  4. Utility & Helper Functions – Math, discount, configuration, or common operations के लिए ideal है।
  5. Initialization Control – Java में static block से variables program start पर initialize कर सकते हैं।

Limitations of Static Members (सीमाएँ)

  1. Cannot store object-specific data – हर object की अलग value store नहीं होती।
  2. Tight Coupling – Overuse से code less modular और tightly coupled हो जाता है।
  3. Cannot access non-static members – Static methods में non-static variables को directly access नहीं किया जा सकता।
  4. Low testing and reusability – Object-specific behavior के लिए flexible नहीं है।
error: Content is protected !!