Why You Should Use Accessors, Not Public Fields
3) You can perform arbitrary side effects
public double setSpeed(double newSpeed) {
speed = newSpeed;
updateSpeedometerDisplay();
}
- If users of your class accessed the fields directly, then they would each be responsible for executing side effects. Too much work and runs huge risk of having display inconsistent from actual values.