Sub topic 1
Class-Level versus Object-Level Members
Java classes can contain members that belong to the class itself and members that belong to each object created from the class. Class-level members use the static keyword. Object-level members are called instance members because each object has its own instance of the member.
Understanding this difference helps you decide whether data should be shared or kept separate.
Prerequisites
You should know how to define a class, declare fields and methods, create objects with new, use access modifiers such as private and public, and call instance methods.
Two Places for State
An instance field stores data for one object. If you create two objects, each object has its own copy of that field.
A static field stores data for the class. The class has one shared copy, regardless of how many objects you create. You can access it through the class name.
public class Counter {
private static int created = 0;
private final int id;
public Counter() {
created++;
id = created;
}
public int getId() {
return id;
}
public static int getCreated() {
return created;
}
public static void main(String[] args) {
Counter first = new Counter();
Counter second = new Counter();
System.out.println(first.getId()); // 1
System.out.println(second.getId()); // 2
System.out.println(Counter.getCreated()); // 2
}
}The id field is an instance field. first and second have different values. The created field is static, so both constructors update the same field.
The static field exists as part of the class's runtime state. Each object still contains its own instance fields.
| Member kind | Declared with | Number of copies | Typical access |
|---|---|---|---|
| Instance member | No `static` keyword | One per object | `object.member` |
| Class member | `static` keyword | One per class | `ClassName.member` |
Static Methods and Instance Methods
A static method also belongs to the class. You can call it without creating an object:
int total = Counter.getCreated();An instance method belongs to an object and usually reads or changes that object's state:
int identifier = second.getId();A static method is appropriate when the operation does not depend on one particular object. For example, getCreated() reports class-wide information. An instance method is appropriate when the operation needs the state of a particular Counter.
Use the class name for static members even when Java permits access through an object reference. Counter.getCreated() clearly communicates that the value is shared. Writing second.getCreated() can make the code look as if the value belongs only to second.
A Mental Model
Think of a class as a design with a class-level area and an object-level area. The class-level area contains one copy of each static field. Every object created from the class receives its own object-level area for instance fields.
This model also explains why changing a static field through one object affects what other objects observe. They all use the same class-level field.
Choosing Between Them
Use an instance member when the value describes one object, such as a user's name or a counter's identifier. Use a static member when the value belongs to the class as a whole, such as a shared count or a class-wide configuration value.
Shared state can be useful, but it creates coupling. Code that changes a static field can affect unrelated objects. This becomes especially difficult when tests run in the same process or when multiple threads update the field. Later chapters examine those risks in detail.
Exercise
In the example, what happens if created is changed from static int to int?
Show answer
Each Counter object gets a separate created field, so the constructor increments a different field for each object. Both objects would produce a created value of 1, and there would be no single class-wide total.