What is the static Keyword in C++?
Learn what the static keyword means in C++ for local variables, globals, and class members, with storage duration, linkage, examples and interview tips.
Expected Interview Answer
The static keyword gives an entity static storage duration (it lives for the whole program) or internal linkage, and its exact meaning depends on where it is used: local variables, global variables, and class members each behave differently.
A static local variable keeps its value between function calls and is initialized only once. A static global variable or function has internal linkage, so it is private to its translation unit. A static class data member is shared by all instances (one copy for the whole class), and a static member function belongs to the class rather than any object, so it cannot access non-static members or use this.
- Preserves state across function calls without a global
- Shares a single value across all objects of a class
- Limits visibility to one file via internal linkage
- Allows class-level functions callable without an instance
- Initializes local statics exactly once, lazily and safely
AI Mentor Explanation
A static local variable is like a team's running total on the scoreboard: each new batter adds to it, but the figure persists between innings rather than resetting per player. A static class member is like the single trophy shared by the whole squad — there is exactly one, not a personal copy for each player, and every member points to the same cup.
Step-by-Step Explanation
Step 1
Static local variable
Declared inside a function; initialized once and retains its value across calls.
Step 2
Static global / function
At file scope, static gives internal linkage, hiding the name from other translation units.
Step 3
Static data member
One shared copy for the whole class; must be defined once outside the class (pre-C++17) or use inline.
Step 4
Static member function
Belongs to the class, not an object; has no this and can only touch static members.
Step 5
Access without an instance
Reach static members via ClassName::member without creating an object.
What Interviewer Expects
- Explains all contexts: local, global, class member, member function
- Knows static locals initialize once and persist
- Knows a static data member is shared across all instances
- Understands internal linkage at file scope
- Knows static member functions have no this pointer
Common Mistakes
- Thinking each object has its own copy of a static data member
- Forgetting to define a non-inline static data member out of class
- Accessing non-static members from a static member function
- Confusing static storage duration with const
- Assuming a static local resets on each function call
Best Answer (HR Friendly)
“In C++, static changes how long something lives and who can see it. A static variable remembers its value between uses, and a static class member is shared by every object instead of each having its own copy.”
Code Example
#include <iostream>
void counter() {
static int calls = 0; // initialized once, persists across calls
++calls;
std::cout << "call #" << calls << "\n";
}
struct Widget {
static int count; // one shared copy for all instances
Widget() { ++count; }
~Widget() { --count; }
static int alive() { return count; } // no 'this' available
};
int Widget::count = 0; // definition outside the class
int main() {
counter(); // call #1
counter(); // call #2
Widget a, b;
std::cout << "alive: " << Widget::alive() << "\n"; // 2
return 0;
}Follow-up Questions
- How does a static local variable differ from a global variable?
- Why must a static data member be defined outside the class?
- Can a static member function access non-static members? Why not?
- What does static mean at file scope versus inside a function?
- How does inline change static data member definitions in C++17?
MCQ Practice
1. How many copies of a static data member exist for a class?
A static data member has a single copy shared by all instances of the class.
2. A static local variable is initialized...
A static local is initialized exactly once, the first time control reaches its declaration, and persists after.
3. Why can't a static member function use non-static members directly?
A static member function is not tied to any object, so it has no this pointer to reach per-object members.
Flash Cards
Static local variable behaviour? — Initialized once, retains its value across function calls.
Static data member? — One copy shared by all instances of the class.
Static member function limitation? — No this pointer; can only access static members.
static at file scope? — Gives internal linkage — the name is private to that translation unit.
How to access a static member? — Via ClassName::member, no object needed.