Javatpoint Logo
Javatpoint Logo

Static Object in C++

C++ is a type of programming language that provides the ability of the programmer to control the system's memory and resources. With the help of C++ programming language, we can develop high-performance applications. In C++, static is a type of method in which we can create the variable, object, and function; then, we can allocate some space for these functions in the programming language for the complete lifetime of a program. Once we can use the static with a method or variable, then it is impossible to modify it again.

In C++, the initialization of the static member function is implemented only once in the whole program. Here the compiler keeps all the variables till the completion of the program. C++ programs allow the programmer to implement the static object inside or outside of the function. In this article, we are going to learn about the different types of static with some examples.

Syntax of C++ Static

The syntax for the static variable is as below.

The syntax for the static function is as below.

Static Variable in a Function

A static variable is a type of variable which allocate space to the function for the lifetime of the program. In C++, once we allocate the space to the static variable, it occupies the space for the program's lifetime.

Even the static variable is called numerous times, but the space allocated for it is constant. And the value of the variable, updated during the last call, is taken for the next call of the static function.

In C++, the static variable is used to implement the co-routine that is used to store the value of the last state of the function. In the below example, we have taken add as a static variable, and it gets updated when the function demo() is being called.

Example 1

Output:

Static Object in C++

Static Variable in the Class

We know a static variable is initialized only once in the program, and it allocates some space to the variable in the program. Because of this, the static variable is shared by every different object. It is impossible to create different copies of a single static variable. In the below example, we have seen that the static variable 'j' is taken and used as explicitly.

Example 2

Output:

Static Object in C++

Static Objects of Class

The objects can also be declared static, as the variables declared in the above examples. When we declare the objects static, it also has the scope for the program's lifetime. In the example below, the object 'nex' has been created in the if block as a static object.

If the object had been created as a non-static object, then the scope of the variable would have been only inside the if block, and as soon as the control of the if the block had gotten over, the destructor would have been invoked. This problem must be avoided, so the object has to be created static as it has been done in the program. This has made the destructor gets invoked once the main has ended. It is only possible because of the static object and its scope throughout the program's lifetime.

Example 3

Output:

Static Object in C++

Conclusion

Based on the above article, we can understand the static concept in C++. The different static methods are discussed in this article with examples and their working. The examples will help understand the concept and use it according to the programmer's requirements.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA