What static Means: Class-Level vs Instance-Level
You'll learn: The fundamental difference between members that belong to the class itself versus members that belong to individual objects.
The Core Distinction
Until now, every field and method you've written belongs to instances—individual objects created with new. Each object gets its own copy of instance fields, and instance methods operate on a specific object's data.
The static keyword changes this completely. Static members belong to the class itself, not to any particular object. There's only one copy of a static field shared by all instances, and static methods can be called without creating any object at all.
Class-Level vs Instance-Level
Think of a class like a blueprint for houses:
- Instance-level (non-static): Each house built from the blueprint has its own address, its own paint color, its own furniture. These are instance variables.
- Class-level (static): The blueprint itself might track "How many houses have been built from this blueprint?" That counter doesn't belong to any one house—it belongs to the blueprint. That's a static variable.
When you mark something static, you're saying "this belongs to the class, period." You don't need an object to access it. Without static, you're saying "every object gets its own version of this."
When to Use Each
- Use instance (non-static) when the data or behavior is unique to each object (like a
Person'snameorage). - Use static when something should be shared across all instances or relates to the class as a whole (like a counter tracking total objects created).
Key Takeaway: Static members are class-level, shared by all instances; instance members are object-level, unique to each object created with new.