# The static Keyword

> The static keyword ties a variable or method to the class itself rather than to any one object. A static class member is a single copy shared across every instance, a static method can be called without creating an instance and has no this reference, and in C or C++ a static local variable keeps its value between function calls.

Canonical: https://devsunite.com/resources/core-subjects/oop/static-keyword
Last updated: 2026-11-22

Subject: OOP · Category: Keywords · 3 min read

The `static` keyword changes how a variable, method, or block relates to the class it's defined in, depending on where it's used. The common thread across all its uses is that `static` ties something to the class itself rather than to any one individual object.

## How it works

**Static variables (class members)**: a single copy is shared across every instance of the class, instead of each object getting its own separate copy. Change it through one object, and every other object sees the change too.

**Static methods**: can be called on the class itself, without creating an instance, and can only directly access other static members (they have no `this`/`self` referring to a specific object).

**Static local variables (inside a function, C++/C specific)**: retain their value between function calls instead of being reset every time the function runs.

## A worked example

```java
class Counter {
    static int totalCount = 0;   // shared across ALL instances
    int id;

    Counter() {
        totalCount++;
        id = totalCount;
    }

    static int getTotalCount() {   // static method, callable without an instance
        return totalCount;
    }
}

public class Main {
    public static void main(String[] args) {
        Counter c1 = new Counter();
        Counter c2 = new Counter();
        Counter c3 = new Counter();

        System.out.println(Counter.getTotalCount());  // prints 3
        System.out.println(c1.id);  // prints 1
        System.out.println(c3.id);  // prints 3
    }
}
```

`totalCount` is a single shared variable, every `Counter` object increments the same one. `id`, on the other hand, is a regular (non-static) field, so each `Counter` object has its own independent copy.

## Where this matters in real systems

Singleton patterns, where exactly one instance of a class should exist for the whole application (a logging service, a configuration manager, a database connection pool), almost always use a static field to hold that single shared instance and a static method to access it. Utility classes like Java's `Math` class are made up entirely of static methods (`Math.sqrt()`, `Math.max()`) because there's no meaningful per-instance state involved at all, just pure functions operating on their inputs.

## What interviewers actually ask about the static keyword

- **"Can a static method access non-static (instance) members?"** No, not directly, a static method has no specific object to refer to, so it can only call other static members unless it's given a specific instance to work with as a parameter.
- **"What's the difference between a static and non-static variable?"** A static variable is shared by every instance of the class; a non-static (instance) variable has a separate copy for each object.
- **"Can you override a static method?"** No, static methods are resolved at compile time based on the reference type, not at runtime based on the actual object, so they can be hidden by a subclass's own static method of the same name, but not truly overridden the way instance methods can be.
- **"Give a real use case for a static variable."** A counter tracking how many instances of a class have been created, or a shared cache/configuration that every instance of the class should see the same version of.

## Key takeaways

- `static` ties a member to the class itself, not to any individual instance.
- Static variables are shared across every object of the class.
- Static methods can be called without creating an instance, and can't directly touch non-static members.
- Static members are the standard building block for singleton patterns and stateless utility classes.

## Related

- [Previous: Composition vs Aggregation vs Inheritance](https://devsunite.com/resources/core-subjects/oop/composition-vs-aggregation-vs-inheritance)
- [Next: The this Keyword](https://devsunite.com/resources/core-subjects/oop/this-keyword)
- [More in Keywords: The const Keyword](https://devsunite.com/resources/core-subjects/oop/const-keyword)
- [More in Keywords: The final Keyword](https://devsunite.com/resources/core-subjects/oop/final-keyword)
- [All 72 OOP interview questions](https://devsunite.com/resources/core-subjects/oop)
