Accessors, Mutators, and the static Keyword
AP Computer Science A · Unit 3 — Class Creation
The only doors into a private object
An accessor (getter) returns a copy of some state and changes nothing: public int getCopies() { return copies; }. A mutator (setter) changes state and normally returns void. The reason a mutator beats a public field is that a mutator can refuse: it validates before it writes, so the object can never enter an impossible state.
A local variable declared inside a method exists only until the method returns, and it is not given a default value — using it before assignment is a compile-time error. An instance variable, by contrast, is initialised to the zero value of its type and lives as long as the object does.
static: one copy for the whole class
A static variable belongs to the class, not to any object: every object shares the same single copy, so a counter incremented in a constructor records how many objects have ever been made. A static method is called on the class — Widget.getMade() — and therefore has no this. That single fact explains the whole rule: a static method cannot read an instance variable or call an instance method directly, because there is no particular object to read from.
The reverse direction is always legal: an instance method may freely use static variables and static methods, since the one shared copy exists no matter which object is asking.
Worked example: a mutator that can refuse, and a static counter
Extend the Book class with: a static variable counting the objects created, a static accessor that reads it, and a mutator setCopies that accepts only non-negative numbers. State what the situation is after three calls to new and one call to setCopies(-5).
Solution
public class Book {
private String title;
private int copies;
private static int made = 0;
public Book(String title, int copies) {
this.title = title;
this.copies = copies;
made++;
}
public static int getMade() {
return made;
}
public void setCopies(int n) {
if (n >= 0) {
copies = n;
}
}
public int getCopies() {
return copies;
}
}After three calls to new Book(...), made equals 3 — not a 1 for each object, because there is only one shared copy that the three constructors increment in turn.
The call b.setCopies(-5) throws no exception and also writes nothing: the condition n >= 0 is false, so the body of the if is skipped and copies keeps its old value. That is exactly the power to refuse that a public field never has.
Note that getMade() is static, so inside its body it may not touch title or copies; it can only read made.
Common mistake
Trap number one: making the counter an instance variable instead ofstatic, then being surprised that every object reports a count of 1. Trap number two, the reverse: making data that is specific to each object static, so that an object created later overwrites the value of an object created earlier. How to prevent it: before typing static, ask exactly one question — if I create a second object, should this value be duplicated? If yes, drop static; if no, keep it.Key takeaway
An accessor reads; a mutator writes with validation.static belongs to the class and has only one copy; a static method has no this so it cannot touch instance variables, while an instance method can touch static variables at any time.