The static keyword and its different meanings

Difficulty: Beginner

Question

What are the different meanings of the static keyword in C++? Explain static local variables, static class members and static at file scope.

Answer

Few keywords are as overloaded as static, and interviewers use that to see if you can keep straight what looks like one concept but is actually a handful. The trick is to sort the uses by context.

First, a static local variable, declared inside a function. It has static storage duration: it is initialised once, the first time control passes through its declaration, and it keeps its value between calls until the program ends. It lives in the data segment, not on the stack. Think of a call counter on a taxi meter that persists after each ride, instead of a fresh trip meter reset every time. Since C++11 the initialisation of function-local statics is guaranteed to be thread-safe, which is the basis of the Meyers singleton, though the object's later use still needs its own synchronisation.

Second, static at namespace or file scope, for a global variable or a free function. Here static gives internal linkage: the name is visible only within its own translation unit (.cpp file), so two files can each have their own static int counter without clashing at link time. In modern C++, an unnamed namespace is the preferred way to achieve the same effect, but you will see static throughout older code and C code.

Third, static data members of a class. A static member belongs to the class rather than to any object, so there is exactly one copy shared by all instances, like a notice board in a hostel that everyone reads, versus each resident's private notebook. It is not counted in sizeof(object). Before C++17, you had to define it outside the class in exactly one .cpp file ('int Counter::total = 0;'), or you'd get a linker error. With C++17, 'static inline int total = 0;' can be defined right in the class. Static const integral members can be initialised in-class; constexpr static members are implicitly inline in C++17.

Fourth, static member functions. These have no this pointer, so they cannot access non-static members directly, cannot be const or virtual, and can be called without an object: 'Counter::count()'. They are used for factory functions, utility functions that relate to a class, and access to static data.

Some pitfalls to raise. The static initialisation order fiasco: the order in which non-local statics in different translation units are initialised is unspecified, so one static must not depend on another in a different file. The fix is to wrap it in a function-local static, which initialises on first use. Also, statics live for the whole program, so they can hold memory or resources for a long time, and static objects' destructors run at exit in reverse order of construction.

Summarise it as: static means 'lives for the whole program' (local variables), 'private to this file' (linkage), or 'belongs to the class, not the object' (members).

Code examples

Static locals and static members

#include <iostream>

int nextId() {
    static int counter = 0;      // initialised once, persists between calls
    return ++counter;
}

class Counter {
public:
    static inline int total = 0;           // one shared copy (C++17 inline)
    Counter() { ++total; }
    static int count() { return total; }   // no 'this'
};

static int fileLocal = 5;                  // internal linkage: this file only

int main() {
    std::cout << nextId() << nextId() << nextId() << "\n";
    Counter a, b;
    std::cout << Counter::count() << " " << sizeof(Counter) << "\n";
    std::cout << fileLocal << "\n";
}

nextId keeps its counter between calls. Counter has no per-object data, so sizeof is 1; the static total is shared and does not add to the object size.

Key points

Common follow-up questions

Interview tips

Concepts covered

static, storage duration, linkage, static members

More C++ & STL questions