A static variable in C++ is a variable with static storage duration: it is created once, lives until the program ends, and keeps its value between function calls. The static keyword also changes linkage and ownership depending on where you write it. Inside a function it makes a persistent local, inside a class it makes one member shared by all objects, and at namespace scope it gives the name internal linkage.
Because the same keyword does three different jobs, most confusion comes from mixing them up. This guide separates each case, shows runnable C++17 and C++20 code, and covers the initialization-order traps that cause real bugs.
A local variable declared static is initialized the first time execution reaches its declaration, and it is never destroyed until the program exits. Its scope is still the enclosing block, so no other function can see it.
A normal local int counter = 0; would be recreated on every call and always print 1. Since C++11, initialization of a static local is thread-safe: if two threads reach the declaration at the same time, one runs the initializer and the other waits. Modifying the variable afterwards is not synchronized, so use std::atomic<int> or a mutex when several threads change it.
Thread-safe lazy initialization makes static locals the simplest way to build a singleton or a lazily created shared resource.
This also avoids the static initialization order problem described later, because the object is built when it is first used rather than before main.
A static data member belongs to the class, not to any instance. There is exactly one copy no matter how many objects exist, and you can reach it without an object.
Since C++17 you can define it inside the class with inline, which removes the need for a separate definition in a .cpp file.
Before C++17 you declared the member in the class (static int live_count;) and defined it in exactly one translation unit (int Widget::live_count = 0;). Forgetting that definition produces an "undefined reference" linker error. A static const or static constexpr integral member can still be initialized in the class, and static constexpr members are implicitly inline since C++17.
A static member function has no this pointer, so it can only touch static members and its parameters. Use it for factory functions and for helpers that do not depend on instance state.
At file or namespace scope, static on a variable or function gives it internal linkage: the name is visible only in that translation unit, so two .cpp files can each define their own static int helper_count without a clash.
Both forms work. The C++ standard no longer treats static for this purpose as deprecated, but an unnamed namespace is the common recommendation because it also applies to types and reads more clearly. Do not confuse this with static inside a function or class, which controls lifetime and sharing rather than linkage.
| Aspect | Non-static local | Static local | Static data member |
|---|---|---|---|
| Lifetime | Until the block ends | Until the program ends | Until the program ends |
| Scope | The block | The block | The class |
| Copies | One per call | One total | One per class |
| Default value | Indeterminate | Zero-initialized | Zero-initialized |
| Storage | Stack | Static storage | Static storage |
Variables with static storage duration are zero-initialized before any other initialization, so static int x; starts at 0, while int x; inside a function holds garbage until you assign it.
The order in which static variables in different translation units are initialized is unspecified. If one global object's constructor uses another global defined in a different .cpp file, it may see an unconstructed object. This is the static initialization order fiasco.
You have three practical fixes:
constexpr or constinit (C++20) so the value is set at compile time and no dynamic initializer runs.Destruction order matters too. Static objects are destroyed in reverse order of construction, and a thread that is still running during exit can touch an already destroyed static. Prefer explicit shutdown over relying on exit-time destruction.
A static variable is shared by all threads. If every thread needs its own copy, declare it thread_local, which gives one instance per thread with the lifetime of that thread.
A global variable has external linkage and can be reached from other files with extern. A static namespace-scope variable is limited to its own file. A static local is not global at all, because its name is visible only inside its block, although it lives for the whole program.
Only its initialization is. Since C++11 a static local is initialized exactly once even when several threads race to it. Reads and writes after that need std::atomic or a lock.
In the static storage area of the program image, typically the data or BSS segment, not on the stack or heap. The exact layout is up to the compiler and platform.
Not directly, because it has no this. It can access them through an object that you pass in as a parameter.
static in C++ means different things by context. A static local persists across calls, a static data member is shared by every object of the class, and a static name at namespace scope has internal linkage. Static variables are zero-initialized, live until program exit, and are shared across threads. Use inline static members in C++17 and later, prefer function-local statics to avoid initialization order problems, and use thread_local when each thread needs its own value.