QuickDevGuide

Home › OOPs

Static vs Instance Members — OOPs

Instance members belong to each object — every Car has its own speed. Static (class) members belong to the class itself and are shared by all instances. Instance Static Accessed via object (car.speed) class (Car.count) Can use this ✅ ❌ Memory one copy per object one copy total Typical use object state & behaviour counters, constants, factory methods, pure utilities When to use static Constants: Math.PI, Integer.MAXVALUE. Factory methods: List.of(), User.fromJson(). Stateless helpers: Collections.sort(), String.join(). Pitfalls Mutable static state is effectively a global variable: it is shared across requests/threads, hard to test and a common source of concurrency bugs. Prefer dependency injection for shared services.

Key Takeaways