--

Setters should perform some kind of validation, otherwise you might just as well not restrict access to the pertinent field.

However, setters are not required, and in some cases should not be provided at all. For example, a bank account object should not allow arbitrary changes to its balance field. Instead, it should receive an immutable transaction object.

It should go without saying that immutable classes shouldn't have setters.

--

--

Alonso Del Arte
Alonso Del Arte

Written by Alonso Del Arte

is a Java and Scala developer from Detroit, Michigan. AWS Cloud Practitioner Foundational certified

No responses yet