C++ static
Last Updated : 11 Jan 2026
In C++, static is a keyword or modifier that belongs to the type not instance. So instance is not required to access the static members. In C++, static can be field, method, constructor, class, properties, operator and event.
C++ uses the static keyword for various functions that include extending variable lifetime duration and reducing scope accessibility while optimizing memory management. The static keyword enables access control for variables and functions as well as class members while managing their behavioural parameters.
Syntax
It has the following syntax:
C++ Static Field
A field that is declared as static is called a static field. Unlike the instance field, which gets memory each time whenever we create an object, there is only one copy of the static field produced in the memory. It is shared with all the objects.
It is used to refer to the common property of all objects such as rateOfInterest in case of Account, companyName, Employee, etc.
C++ Static Field Example
Let us take a simple example of a static field in C++.
Output:
101 John 7.25 102 Alice 7.25
C++ Counting Objects Examples Using Static Keyword
Let us take another example of static keyword in C++ that counts the objects.
Output:
101 John 102 Alice 103 Michael 103 Michael Total Objects are: 4
Here, we will discuss several static function that can be utilized in C++.
1. Static Variables in Functions
In C++, static variable contains determined values throughout separate function executions while avoiding continuous initializations. Static variables in function are initialized only once, and then these variables hold values even through function calls.
C++ Static Variables in Functions Example
Let's look at an example to demonstrate the static variable in functions in C++.
Output:
Count: 1 Count: 2 Count: 3
Explanation:
In this example, a variable count is declared as static. Therefore, the value is carried via the function calls. The variable count is not getting initialized every time the function is called.
2. Static Data Members in Classes
In C++, the static data members are class members that are defined using the static keyword. It is shared among all objects of the class and belongs to the class, not separate objects. There may not be multiple copies of the same static variable for several objects.
C++ Static Data Members in Classes Example
Let's look at an example to demonstrate static data members in the class in C++.
Output:
3. Static Member Functions in a Class
In C++, static member functions operate independently from objects of any class, and they can only access static class elements.
C++ Static Member Function in a Class Example
Let's look at an example to illustrate static member function in the class in C++.
Output:
Explanation:
The function updates the increment () static variable count, which retains its value in several function calls, and is called without creating an object.
4. Global Static Variables
A static global variable retains an internal connection that confines its usage to the particular file where statements declare it. The internal linkage of static variables protects them from conflicts that occur between different files.
C++ Global Static Variables Example
Let's take an example to demonstrate the global static variables in C++.
Output:
Global Counter: 1 Global Counter: 2
Explanation:
Globalcounter retains its value in variable function call, but due to static keywords remain limited to this source file.
5. Static Variables in Namespaces
A static variable within a namespace works similarly to a static global variable while maintaining its usage scope to the current file.
C++ Static Variables in Namespace Example
Let's take an example to demonstrate the static variables in namespaces in C++.
Output:
Count Number: 1 Count Number: 2
6. Static Local Variables in Loops
In C++, a static variable inside a loop retains its value across iterations.
C++ Static Local Variables in Loops Example
Let's look at an example to demonstrate the static local variabel in loops in c++.
Output:
Loop Iteration: 1 Loop Iteration: 2 Loop Iteration: 3 Loop Iteration: 4 Loop Iteration: 5 Loop Iteration: 6
7. Static in Multithreading
In C++, static variables in multithreaded programs trigger data races because threads share their values.
C++ Static in Multithreading Example
Let's look at an example to demonstrate the static in multithreading in C++.
Output:
Conclusion
In conclusion, the static keyword functions within C++ programming language enable users to execute effective memory management while maintaining encapsulation rules alongside shared data cooperation.
Static allows programmers to benefit from essential functionality, which includes state retention within functions class-level data sharing and variable scope constraints. Threaded environments require special management of data to prevent racing conditions.