Sitemap

Understanding static and Why It's the Backbone of Singleton

--

1. What static Actually Means

  • Binds to the class, not the object. A static member belongs to the class itself — every object shares the same copy. A normal (instance) member gets a fresh copy per object.
  • No object needed to access it. You can call ClassName::staticMember without ever creating an instance.
  • Lifetime = entire program. Static variables are created once (at program start / first use) and destroyed only when the program ends — not tied to any object’s lifetime.

2. Normal Variable vs Static Variable

Normal (Local/Instance) Variable

  • Memory: A fresh copy is created every time — with each function call or each new object
  • Lifetime: Exists only temporarily; destroyed once the function ends or the object goes out of scope
  • Value Across Calls: Starts fresh every time — has no memory of previous calls

Static Variable

  • Memory: Only one copy exists in memory, shared across all calls or all objects
  • Lifetime: Persists for the entire runtime of the program
  • Value Across Calls: Retains its last updated value — continues from where it left off

Example — static variable inside a function “remembers” state:

void counter() {
static int count = 0; // initialized only ONCE, ever
count++;
std::cout << count << std::endl;
}
int main() {
counter(); // 1
counter(); // 2
counter(); // 3 -> picks up where it left off
}

A normal local variable would print 1, 1, 1 every time because it's re-initialized on every call. A static one keeps its state across calls — that's the "starts from where it left off" behavior.

Get Prasadam’s stories in your inbox

Join Medium for free to get updates from this writer.

Same idea for a static class member: all objects of the class read/write the same variable.

class Counter {
public:
static int count; // shared across ALL objects
Counter() { count++; }
};
int Counter::count = 0; // must define outside the class
int main() {
Counter a, b, c;
std::cout << Counter::count; // 3, shared by all instances
}

3. Why Singleton Needs static

Goal of Singleton: exactly one instance of a class should exist, and everyone should access that same instance.

Problem: How do you keep track of “the one instance” without needing an object to store it in? (Chicken-and-egg problem — storing it as a normal member would need an object to exist first.)

Answer: Store the instance pointer as static (class-level, no object required), and expose a static method to fetch/create it.

Clean, corrected version of your code

class Singleton {
private:
Singleton() {} // private constructor — blocks `new Singleton()` from outside
static Singleton* instance; // one shared pointer for the whole class
public:
static Singleton* getInstance() {
if (instance == nullptr) {
instance = new Singleton();
}
return instance;
}
};
// static members must be defined outside the class
Singleton* Singleton::instance = nullptr;
int main() {
Singleton* instance1 = Singleton::getInstance();
Singleton* instance2 = Singleton::getInstance();
// instance1 == instance2 -> same object, guaranteed
}

--

--