Why You Should Use Accessors, Not Public Fields
1) You can put constraints on values
public void setSpeed(double newSpeed) {if (newSpeed < 0) {
sendErrorMessage(...);
newSpeed = Math.abs(newSpeed);
}
speed = newSpeed;
}
- If users of your class accessed the fields directly, then they would each be responsible for checking constraints.