# The static keyword and its different meanings

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

Canonical: https://www.internhack.in/learn/interview/cpp-interview/cpp-static-keyword
Last updated: 2026-11-28

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

- Static local: initialised once, lives for the whole program, thread-safe initialisation since C++11.
- Static at file scope: internal linkage, visible only in that translation unit.
- Static data member: one copy per class; define in a .cpp before C++17 or use static inline.
- Static member functions have no this pointer and cannot be virtual or const.
- Static initialisation order across translation units is unspecified.

## Common follow-up questions

- What is the static initialisation order fiasco and how do you avoid it?
- How would you implement a Meyers singleton?
- Can a static member function access non-static members?

## Interview tips

- Group the meanings by context (function, file, class) instead of listing randomly.
- Mention thread-safe function-local statics to show C++11 awareness.

## Concepts covered

static, storage duration, linkage, static members

Previous: [Lambdas and capture modes](https://www.internhack.in/learn/interview/cpp-interview/cpp-lambdas-captures) · Next: [Undefined behaviour: common examples and how to avoid it](https://www.internhack.in/learn/interview/cpp-interview/cpp-undefined-behaviour) · [All of C++ & STL](https://www.internhack.in/learn/interview/cpp-interview)

## More C++ & STL questions

- [Tricky C++ output questions](https://www.internhack.in/learn/interview/cpp-interview/cpp-tricky-output-questions)
- [What are the key differences between C and C++, and between struct and class?](https://www.internhack.in/learn/interview/cpp-interview/cpp-c-vs-cpp-struct-class)
- [References vs pointers in C++](https://www.internhack.in/learn/interview/cpp-interview/cpp-references-vs-pointers)
- [const pointer vs pointer to const, and const correctness](https://www.internhack.in/learn/interview/cpp-interview/cpp-const-correctness)
- [Stack vs heap memory in C++](https://www.internhack.in/learn/interview/cpp-interview/cpp-stack-vs-heap)
- [new/delete vs malloc/free](https://www.internhack.in/learn/interview/cpp-interview/cpp-new-delete-vs-malloc-free)

## Related

- [Learn](https://www.internhack.in/learn)
- [Interview Prep](https://www.internhack.in/learn/interview)
- [C++ & STL](https://www.internhack.in/learn/interview/cpp-interview)
