O'Reilly logo
  • You Wang thinks this is interesting:

The salary field is not read-only, but it can only be changed by the raiseSalary method. In particular, should the value ever turn out wrong, only that method needs to be debugged. Had the salary field been public, the culprit for messing up the value could have been anywhere.


Cover of Core Java® Volume I—Fundamentals, Tenth Edition


With a setter, object can restrict the value changes to a certain field with rules. This can't be done if the field is public.