Fundamentals · Medium · Free
What does static mean for variables and methods?
FresherFundamentalsOOP & Language Fundamentals
static members
static belongs to the class, not each instance—one copy shared by all objects. Static methods cannot access instance fields directly.
- static fields: counters, caches, constants (with care for thread safety).
- static methods: utilities (
Math.max)—nothis. - Static blocks run once when class loads.
java
class IdGenerator {
private static int next = 1;
static int nextId() { return next++; }
}Warn about static mutable state in web apps—hard to test and breaks in clustered servers.