Getters and Setters

  • Using a getter to return an imutable type (ie list or custom type), should return a clone of the object, to keep object immutable

    • Also be aware object graph having mutable types

  • The idea is that getters encapsulate how values are represented in an object, providing a consistent approach across a codebase.

  • It also allows for protection against aliasing, for example, by cloning collections before returning them

Reasons against

Last updated

Was this helpful?