Static Keywords in C++

Static keyword in C++: Static Variables, Static Member of Calss

In C++, static keyword can be used in a number of ways as explained below. Static variables, Static Data Members of a Class and Static Members Functions of a Class.

‘Static’ keyword is used to make use of static memory. Static memory remains live till the end of the program execution. If the element of a program is declared as static then it will use this special type of static memory and will keep its value available as long as the program in executing.

In C++, static keyword can be used in a number of ways as explained below.

Static Variables

If a variable in declared in a function then it will occupy a memory during the execution of the function and will release the memory as soon as the function ends as shown below.

release the memory

The output of the above code is as below. It is important to note that variable ‘I’ is declared as non-static at line 8 and thus will be re-initialized every time the function abc() is called.

re-initialized

But in contrast if the variable in above function is declared as static then it will retain its value till the program lifetime and will not be re-initialized every time on function call as shown in the following example.

static

The output of the above code is as below. It is important to note that variable ‘I’ is declared as static this time at line 8 and will retain its last value when the function abc() is called.

abc()

Static Data Members of a Class

Static data members of a class are shared among all the objects of a class. All the objects of the same class access these variables from the static memory of the class and thus individual copies of these variables are not given to each object of the class. It also means that if an object of the class changes the value of a static data member then this change is also obtained by all the objects of the same class as shown in the following example.

It is important to note that static data members of a class are not initialized by the constructors of the class rather they are initialized outside the class as mentioned at line 14 in the following code.

line 14

In the above example, static data member is declared at line 9 and initialized at line 14. We can see that the static data member is assigned a value ‘1’ at line 18 by obj1 of the class but when we display the same variable by obj2 at line 22 then the same value is printed as assigned by obj1 which means that both obj1 and obj2 share the same values of variable a. The output of the above code is as below.

output

Static Members Functions of a Class

Static member functions of a class can be accessed directly without defining object of the class or through the object of the class. For instance display() is declared as static in the following example and called at lines 21 and 22. At line 21, it is called by using the object of the class while at line 22 it is called directly without using the object of the class. If the same method is declared as non-static then we will get an error at line 22 as non-static functions are not allowed directly.

line 22

The output of the above code is as below.

hi

More Related Articles For You

    C++ Tutorial

    C++ Quizzes