RSS Feed Subscribe to RSS Feed



Immutable classes are inherently thread safe, and can only ever be in a single state. A class can be made immutable by:

  • all fields being final and private
  • no mutator (setter) methods
  • class can’t be extended (e.g. make final) to avoid subclasses making things mutable
  • provide exclusive access to any mutable components (e.g. getters provide defensive copies of collections)

See Item 15:Minimize mutability in Effective Java for more details.

Tags: ,