Static Members of Class
In C++, static members of a class are members that belong to the class itself rather than to any specific object of the class. There are two types of static members in a class: static data members & static member functions.
Static Data Members
Static data members are variables that are shared among all objects of a class. They are declared within the class but defined outside the class using the scope resolution operator (::).
For example:
class Circle {
public:
static const double PI;
double radius;
Circle(double r) {
radius = r;
}
double getArea() {
return PI * radius * radius;
}
};
const double Circle::PI = 3.14159;
In this example, 'PI' is declared as a static const data member of the 'Circle' class. It is defined outside the class definition using the scope resolution operator. The 'PI' value remains constant throughout the program & is accessible to all objects of the 'Circle' class. The 'getArea()' function uses the 'PI' value to calculate the area of a circle.
Static Member Functions
Static member functions belong to the class and can be called without creating an object of the class. They can only access static data members and other static member functions of the class.
For example:
class MathUtils {
public:
static int sum(int a, int b) {
return a + b;
}
};
int main() {
int result = MathUtils::sum(5, 7);
cout << "Sum: " << result << endl;
return 0;
}

You can also try this code with Online C++ Compiler
Run Code
In this example, the 'sum()' function is declared as a static member function of the 'MathUtils' class. It takes two integers as parameters and returns their sum. The 'sum()' function can be called directly using the class name and scope resolution operator without creating an object of the class.
Static Functions in a Class
Static functions in a class are member functions that can be called without creating an object of the class. They belong to the class itself rather than to any specific object. Static functions can only access static data members & other static functions of the class. They cannot access non-static members directly.
For example:
class Calculator {
private:
static int count;
public:
static void incrementCount() {
count++;
}
static int getCount() {
return count;
}
};
int Calculator::count = 0;
int main() {
Calculator::incrementCount();
Calculator::incrementCount();
Calculator::incrementCount();
cout << "Function called " << Calculator::getCount() << " times." << endl;
return 0;
}

You can also try this code with Online C++ Compiler
Run Code
In this example, the Calculator class has a static data member, count, and two static member functions, incrementCount() and getCount(). The incrementCount() function increments the value of 'count' each time it is called, while getCount() returns the current value of 'count'.
In the 'main()' function, the 'incrementCount()' function is called three times using the class name and scope resolution operator without creating any objects of the class. Finally, the 'getCount()' function is called to retrieve the value of 'count', which will be 3.
Note: Static functions are useful when you need to perform operations that are related to the class as a whole rather than to specific objects of the class. They can be used to maintain global state, counters, or utility functions that don't require access to object-specific data.
Advantage of C++ Static Keyword
1. Memory Efficiency: Static variables are allocated memory only once, at the start of the program execution, & they persist throughout the lifetime of the program. This means that static variables consume memory only once, regardless of how many times the function or block in which they are defined is executed. This can lead to more efficient memory usage, especially when dealing with large numbers of function calls or objects.
2. Data Sharing: Static variables provide a way to share data between multiple function calls or objects of a class. Since static variables retain their values between function calls, they can be used to maintain state or counter variables that need to be accessed & modified by multiple functions or objects. This eliminates the need to pass data explicitly between functions or objects, making the code more concise & easier to understand.
3. Encapsulation: Static member functions in a class can be used to encapsulate functionality that belongs to the class as a whole, rather than to specific objects of the class. This promotes a more organized & modular design, as related utility functions or operations can be grouped together within the class. Static member functions can access static data members, allowing for centralized control & management of class-level data.
4. Global Access: Static variables & functions in a class can be accessed using the class name & scope resolution operator, without the need to create objects of the class. This provides a convenient way to access class-level data & functionality from anywhere in the program, without the overhead of object creation. It allows for cleaner & more readable code, as the static members can be accessed directly using the class name.
5. Constant Values: Static const variables in a class can be used to define constant values that are shared among all objects of the class. This is useful for defining mathematical constants, configuration values, or other fixed quantities that are relevant to the class. By declaring them as static const, you ensure that their values cannot be modified & that they are accessible to all objects of the class.
Examples
Let's look at a few examples to understand the implementation properly:
Example 1: Static Variable in a Function
void countCalls() {
static int count = 0;
count++;
cout << "Function called " << count << " times." << endl;
}
int main() {
countCalls();
countCalls();
countCalls();
return 0;
}

You can also try this code with Online C++ Compiler
Run Code
Output:
Function called 1 times.
Function called 2 times.
Function called 3 times.
In this example, the `countCalls()` function has a static variable `count` that keeps track of the number of times the function is called. Each time the function is called, `count` is incremented & its value is printed. Since `count` is static, it retains its value between function calls, allowing us to count the total number of calls.
Example 2: Static Member in a Class
class Student {
public:
static int totalStudents;
Student() {
totalStudents++;
}
};
int Student::totalStudents = 0;
int main() {
Student s1;
Student s2;
Student s3;
cout << "Total students: " << Student::totalStudents << endl;
return 0;
}

You can also try this code with Online C++ Compiler
Run Code
Output:
Total students: 3
In this example, the `Student` class has a static data member `totalStudents` that keeps track of the total number of student objects created. Each time a `Student` object is constructed, the `totalStudents` count is incremented. The `totalStudents` member is accessed using the class name `Student` & the scope resolution operator `::`. Finally, the total number of students is printed, which is 3 in this case.
Example 3: Static Member Function in a Class
class MathUtils {
public:
static int square(int x) {
return x * x;
}
};
int main() {
int result = MathUtils::square(5);
cout << "Square of 5: " << result << endl;
return 0;
}

You can also try this code with Online C++ Compiler
Run Code
Output:
Square of 5: 25
In this example, the `MathUtils` class has a static member function `square()` that calculates the square of an integer. The `square()` function is called directly using the class name `MathUtils` & the scope resolution operator `::`, without creating any objects of the class. The result of `square(5)` is printed, which is 25.
Frequently Asked Questions
Can static member functions access non-static members of a class?
No, static member functions can only access static members of the class directly. They cannot access non-static members without an object.
Can we have multiple static variables with the same name in different functions?
Yes, static variables in different functions are independent of each other, even if they have the same name.
Is it necessary to initialize static data members outside the class?
Yes, static data members must be initialized outside the class definition using the scope resolution operator (::).
Conclusion
In this article, we have learned about the static keyword in C++ and its various applications. We explained static variables, which retain their values throughout the program's execution, and static variables in classes, which are shared among all objects of the class. We also discussed static member functions, which can be called without creating an object, and the advantages of using the static keyword, like memory efficiency and data sharing.
You can also check out our other blogs on Code360.