DRAFT
SetterIsStatic
Misconception:
A setter method of an instance variable needs to be a class method.
Incorrect
Setter methods are static
Correct
CorrectionHere is what's right.
Here is what's right.
A setter method (which sets an instance variable) needs to be an instance method. Otherwise it couldn’t possibly access the instance variable.
Example Occurrences
public class IntHolder {
private int a;
public IntHolder(int a) {
this.a = a;
}
public static void setter(int input) {
a = input; // could not assign to instance variable a
}
public int getter() {
return a;
}
}
public class IntHolder {
public int num;
public IntHolder(int num) {
this.num = num;
}
public static void setter(int newN) {
this.num = newN; // this does not exist in static methods
}
public int () { // missing method name?
return num;
}
}