‘static’ keyword in C++

By | August 9, 2020

Almost each & every object oriented programming language is equipped with a special feature known as ‘static’ keyword. ‘static’ is such a keyword which can be used or written in front of any member variable or member function of a class.

Now, what happens to that member variable or function if it is preceded by that ‘static’ keyword? That particular field or behavior becomes absolute property of the class to where it belongs. Yes, it no longer remains an individual or separate property of each object of that class. The interesting fact is that every object of that specific class shares a single copy of that attribute or member function. If lacs of objects are created then also only a unique copy of that entity is maintained.

Note –

  • Suppose we have created six objects of a class named ‘Demo’.
  • That ‘Demo’ class has a static member entity (be it a member variable or member function).
  • Then Object1, Object2 … Object10 will share a single copy of that static entity. Those objects will not be having separate copies of the static entity. The number of objects of a class may be innumerable but the principle never changes.

It must be clear that static entities are the properties of the class. ‘static’ keyword can help us in writing better & relevant codes wherever appropriate.

  1. Static Member Variables –
    If any member variable within a class is preceded by ‘static’ keyword then it becomes static member variable also known as class variables. Like any other member variable it can be either private or protected or public. Whenever we declare any member variable as ‘static’ it is initialized with the default value of zero. It can be accessed by the class name unlike other member variables which can be used or initialized using individual object names. In C++ ‘static’ member variables are initialized outside the scope of the class using scope resolution operator (::).

    For example, if one famous & well known use of static member variable is keeping track of the number of objects created of a particular class.

  2. Static Member Functions –
    Like member variables we can also create or write ‘static’ member functions. Just what we have to do is the corresponding function header must start with the word ‘static’. There are some certain characteristics of such functions which should be kept in mind while working.

    • ‘static’ member functions are properties of the class & it can only be called using that class name. So these functions are also called class functions.
    • These functions can only directly access ‘static’ member variables & call other ‘static’ member functions.


    For example, if there are various uses of ‘static’ functions. When we are working with ‘static’ member variables then there is no point in initializing or manipulating it using non-static member functions because every object can have its own way of using those static variables but we can’t allow such haphazard manipulations as it can result to some data inconsistencies.



    Note –
    One of the most famous & vastly used object oriented programming language Java has got a special feature called ‘static block’. ‘static block’ is used to initialize ‘static’ data members at the time compilation. Although, it’s a very useful characteristic that Java has C++ programming language has no such feature.

    Now, after discussing use of ‘static’ keyword in C++ programming language elaborately one thing must be crystal clear & there lies no doubt that ‘static’ keyword is only used whenever something needs to be used in the context of classes. As ‘static’ keyword makes the specific member variable or member function independent of any particular object we must be cautious & careful while using ‘static’ keyword. And also ‘static’ feature can’t be used anywhere, casually & aimlessly.

    #include <iostream>
    using namespace std;
    
    // class demo
    class Demo {
    private:
        // Static member variable of class
        static int count; 
    public:
        // Whenever an object of 'Demo' is created, 
        // the value of 'count' is increased by 1 
        // within the constructor
        Demo() 
        {
            count++;
        }
        // Static member function 
        // to show number of objects 
        // created for this particular class
        static void showNumberOfObjectsCreated() 
        {
            cout << "Number of objects created=" << count << endl;
        }
    };
    // Initialization of the static member variable
    int Demo::count = 0; 
    
    // Driver program 
    int main()
    {
        // Calling of static member function
        Demo::showNumberOfObjectsCreated(); 
        // 1st object of 'Demo' class created
        Demo ob1; 
        Demo::showNumberOfObjectsCreated();
        // 2nd object of the class created
        Demo ob2; 
        Demo::showNumberOfObjectsCreated();
        return 0;
    }
    

    Output :-

    Number of objects created=0
    Number of objects created=1
    Number of objects created=2 



    Please write comments if you find anything incorrect. A gentle request to share this topic on your social media profile.