Introduction to C++ | static Keyword

C++ static Keyword

The static keyword in C++ is used to manage the lifetime, scope, and visibility of variables and functions. The static keyword has different behaviors when applied to variables inside a function, class members, and global or namespace scope. Understanding how to use static can help in memory management and achieving better program optimization.

What Does Static Do?

The behavior of the static keyword differs depending on where it is used:

Syntax of Static Keyword

The general syntax for the static keyword is as follows:

Syntax of Static


        static type variable_name;  // static variable in function or class
        static return_type function_name() {  // static function
            // function body
        }
                    

Static Variable in a Function

When the static keyword is applied to a local variable inside a function, the variable retains its value between function calls. Unlike regular local variables, which are created and destroyed with each call, a static local variable is initialized only once and exists for the lifetime of the program.

Example: Static Variable in Function

Here’s an example of a static variable inside a function:

Code Example: Static Variable Inside Function


        #include <iostream>
        using namespace std;
        
        void countCalls() {
            static int count = 0;  // Static variable
            count++;
            cout << "Function called " << count << " times." << endl;
        }
        
        int main() {
            countCalls();  // Output: Function called 1 times.
            countCalls();  // Output: Function called 2 times.
            countCalls();  // Output: Function called 3 times.
            
            return 0;
        }
                    

Output:

Function called 1 times.
Function called 2 times.
Function called 3 times.

Static Member Variables

A static member variable is shared by all objects of a class. It is not tied to any specific instance of the class and has the same value for all objects of that class. Static member variables are initialized outside the class definition.

Example: Static Member Variables

Here’s an example of using static member variables in a class:

Code Example: Static Member Variables


        #include <iostream>
        using namespace std;
        
        class Counter {
        public:
            static int count;  // Static member variable
        
            // Constructor
            Counter() {
                count++;
            }
        
            // Display function
            void displayCount() {
                cout << "Current count: " << count << endl;
            }
        };
        
        // Initializing static member variable outside the class
        int Counter::count = 0;
        
        int main() {
            Counter c1;
            c1.displayCount();  // Output: Current count: 1
        
            Counter c2;
            c2.displayCount();  // Output: Current count: 2
        
            return 0;
        }
                    

Output:

Current count: 1
Current count: 2

Static Member Functions

Static member functions are functions that are shared by all objects of a class. These functions can only access static member variables or other static functions. They do not have access to the non-static members of the class because they are not associated with any specific object.

Example: Static Member Function

Here’s an example of a static member function:

Code Example: Static Member Function


        #include <iostream>
        using namespace std;
        
        class Counter {
        public:
            static int count;  // Static member variable
        
            static void displayCount() {  // Static member function
                cout << "Current count: " << count << endl;
            }
        
            Counter() {
                count++;
            }
        };
        
        int Counter::count = 0;
        
        int main() {
            Counter c1;
            Counter::displayCount();  // Output: Current count: 1
        
            Counter c2;
            Counter::displayCount();  // Output: Current count: 2
        
            return 0;
        }
                    

Output:

Current count: 1
Current count: 2

Static Variables and Functions in Global Scope

When the static keyword is used at the global level (outside any function or class), it restricts the scope of a variable or function to the file in which it is defined. This prevents name conflicts and ensures that the variable or function cannot be accessed by other files, even if they are part of the same project.

Example: Static Global Variables

Here’s an example of using static at the global scope:

Code Example: Static Global Variable


        #include <iostream>
        using namespace std;
        
        static int count = 0;  // Static global variable
        
        void incrementCount() {
            count++;
            cout << "Count: " << count << endl;
        }
        
        int main() {
            incrementCount();  // Output: Count: 1
            incrementCount();  // Output: Count: 2
        
            return 0;
        }
                    

Output:

Count: 1
Count: 2

Pro Tip:

💡 Pro Tip

The static keyword is very useful when you want to preserve the state of a variable between function calls or share a variable across multiple objects in a class. When using static in global scope, it helps avoid naming conflicts by limiting access to the variable or function within the file.