Lesson 12: Static vs Instance Members

13 Jul 2026 1 min Swarnil Singhai

Static members belong to the class itself, shared across every instance; instance members belong to each object separately.

Real-time example

A Counter class tracks how many User objects have ever been created using one shared static field.

class User {
    static int totalUsers = 0;
    String name;

    User(String name) {
        this.name = name;
        totalUsers++;
    }
}
new User("Amit"); new User("Riya");
System.out.println(User.totalUsers); // 2

What's happening

totalUsers exists once in memory no matter how many User objects are created — every instance increments the same counter.

Use static for things that belong to the concept of the class (a counter, a constant), not to any one instance.

Advertisement