A class defines state and behavior; an object is one instance of that definition.
Java classes and objects
Working model
Keep state private when arbitrary writes would violate an invariant. The public operations below permit a task to change status without exposing the stored boolean as a writable field.
Code
public class DispatchTask {
private boolean completed;
public void complete() {
completed = true;
}
public boolean isCompleted() {
return completed;
}
public static void main(String[] args) {
DispatchTask task = new DispatchTask();
task.complete();
System.out.println(task.isCompleted());
}
}The output is true. Each task has its own completed field. A static field would instead belong to the class and would be shared by all instances.
Cost and design choices
Objects are accessed through references. Assigning task to another variable does not clone its state. Make copying explicit when a separate lifecycle is required.
Reachability and object ownership
A constructor does not automatically deep-copy the values passed into it. Storing a caller’s mutable collection keeps a shared reference. If the object promises a snapshot, copy the relevant structure and state how mutable elements are handled.
Garbage collection reclaims unreachable objects, not objects merely labelled temporary in a method name. Static fields, listeners, and captured callbacks can keep entire object graphs reachable. Releasing a reference also does not promptly close an external file or socket.
Connected lessons
Continue with Java constructors and validated state, Java Tutorial.
Common Mistakes
Do not expose mutable fields simply to reduce accessor code. A null reference does not identify an object. Calling an instance method through it fails before the method can inspect its arguments.
Make object state valid at publication
A constructor should establish the invariant that every permitted public operation can rely on. A stock object with a negative available count is already invalid before reserve is called. Reject that input rather than requiring every future operation to repair it.
An object can be referenced by several components. Returning its mutable internal list gives callers another mutation path around the class’s validation. Publish a copied or explicitly read-only view according to the required lifetime; a read-only live view still changes when the owner changes its backing state.
Instance fields belong to a particular object, while static fields belong to the loaded class. Moving state to static can accidentally merge independent accounts or requests. It also adds a shared lifetime and coordination requirement that did not exist when each request owned its own object.
Apply this contract in Spring
Spring ApplicationContext: construct and close an object graph, Spring bean scopes: singleton identity is not thread safety. These lessons keep framework assembly separate from the Java contract.
Next boundary checks
Continue with Java nested classes: instance capture versus static ownership.
