Cosmic Module
J
Qubits of DPK
March 14, 2026
Core Java
Layman Explanation
Imagine a school with 50 students. Every student has their own name and roll number. But the school name is SAME for all. Why store "DPS School" 50 times? Store it ONCE and let all students share it. That's a static variable.
What is a Static Variable?
A static variable belongs to the CLASS — not to any individual object. All objects share the SAME single copy.
java
QUBITS OF DPK
Memory Model
javascript
QUBITS OF DPK
Full Example
java
QUBITS OF DPK
Accessing Static Variable
java
QUBITS OF DPK
Production Use Cases
java
QUBITS OF DPK
️ All Traps
java
QUBITS OF DPK
Interview Answer (SDE-2)
"A static variable belongs to the class rather than any object — stored in Method Area, shared by all instances, initialized when class loads. Instance variables have separate copies per object; static variables have one copy for the entire class. Access via class name to make shared nature explicit. Used for counters, constants, configuration values, and Singleton patterns. In a payment service, gateway URL should be static final — same for every transaction, no reason to create per object."
Interview Questions & MAANG-Level Answers
Q1. What is a static variable and where is it stored?
A static variable belongs to the class itself, not to any instance. It is stored in the Method Area (part of JVM memory), initialized when the class is loaded, and lives until the program ends. All instances share the same copy. Example: static int totalUsers = 0 — incremented in constructor to count all objects ever created.
Q2. What is the difference between static and instance variables?
Instance variables: each object gets its own independent copy, stored inside the object in heap, initialized per object creation, dies when object is garbage collected. Static variables: ONE copy shared by all objects, stored in Method Area, initialized once when class loads, lives for the entire program duration. Example: name (instance — each user has their own name), totalUsers (static — one count for all users).
Q3. Can you change a static variable? What happens to other objects?
Yes, static variables can be changed (unless final). When changed, ALL objects immediately see the new value since they all share the same copy. Student.schoolName = "New School" — every Student object now sees "New School" when accessing schoolName. This shared mutability can cause bugs in multi-threaded code — use synchronized or AtomicInteger for thread-safe static counters.
Q4. What is static final and when do you use it?
static final is a class-level constant — one copy, never changes. Best practice for magic numbers and configuration values:
java
QUBITS OF DPK
By convention, use UPPER_SNAKE_CASE. Evaluated at compile time if primitive — the compiler may inline the value directly.
Q5. Why should you access static variables via class name?
Accessibility and clarity. Student.schoolName makes it immediately obvious that schoolName is a class-level (shared) variable. s1.schoolName looks like an instance variable — misleading to readers and maintainers. Most IDEs and code linters warn when accessing static members via instance reference. Always prefer ClassName.staticMember for readability and correctness.