All about Static Keyword in C++

In simple words, static means something which does not change with time, which once allocated does not change throughout the lifetime of the program. It will remain in the memory till the program runs. They carry their value, even if they are out of scope. For Example Your Name, your age changes, your place changes, your education change, and your job change, but your name remains constant in your lifetime, in the same way, static variables and functions do not change even if they move out of scope in their lifetime.

Syntax:

static data_type var_name = var_value;

Essential Properties of the Static variables:

  • The static variable remains in the memory, not like the one which is destroyed when they are out of scope.
#include <iostream>
using namespace std;
//function with the static keyword
int function(){
    static int number=0;
    number++;
    return number;
}
//function with non-static keyword
int function2(){
    int number2=0;
    number2++;
    return number2;
}

int main(){
    //calling function with static keyword integer
    cout<<"First Call of Function with static variable: "<<function()<<endl;
    cout<<"Second Call of Function with static variable: "<<function()<<endl;
    //calling function with nonstatic keyword integer
    cout<<"First call of Function with non-static variable"<<function2()<<endl;
    cout<<"Second call of Function with non-static variable"<<function2()<<endl;
    return 0;
}

Online C++ Compiler (2).png

  • They are never allocated in stack memory, rather are stored in the data segment.
  • If you don’t initialize the static memory with any value, they are initializing with 0.
     #include<iostream>
      using namespace std;
      int main(){
            int a;
            static int b;
              cout<<"Value of static variable: "<<a<<endl;
              cout<<"Value of non-static variable: "<<b<<endl;
              return 0;    
 }

Online C++ Compiler (3).png

  • They always have to be initialized outside of a class.
    #include<iostream>
    using namespace std;
    class Human{
            public:
                static int count;
                Human(){
                        count++;
                    }
    };
    int Human::count =0;

    int main(){
        cout<<Human::count<<<endl;
        return 0;
    }

Online C++ Compiler (4).png

  • Static variables can be used even if no object is created. Unlike members of the class who can only be accessed when an object is created.
    #include<iostream>
    using namespace std;
    class Human{
            public:
                static int count;
                Human(){
                        count++;
                    }
    };
    int Human::count =0;

    int main(){
        cout<<Human::count<<<endl;
        return 0;
    }

Online C++ Compiler (4).png

Some Important Interview Questions:

Ques: Can I access static data members without creating an object?

Ans: Yes, because they are not tied to a specific instance.

Ques: Can we mark a local variable as static?

Ans: No, we can not mark a local variable with a static keyword.

Ques: When does a static keyword get memory?

Ans: When a class loaded into memory at runtime.

Ques: Can we use this keyword in the Static method?

Ans: No, we cannot use this keyword.

Ques: Can we override the static method?

Ans: No.

Ques: Can we declare a static variable in the class?

Ans: No, we cannot declare a static variable in the class, we have to do it outside the class, outside any function.

STATIC FUNCTIONS

Static functions are functions that can change the static data members but cannot change the value of the non-static data members.

A non-static member function can modify a static data member as long as the data member visibility is allowed.

Static Keyword does not have this keyword, because this points to current objects, but the static method does not have things.

Thank You for reading, Hope you learn something new today. Do like and follow for more.

You can connect with me: