NOTE

Immutable Object

How to create an immutable object.

JavaCreated Updated 1 min readhistorical

This is a historical learning note and may contain outdated or incomplete understanding.

How to Create an Immutable Object

  • Apply final to the class.
  • Apply final to internal fields.
    • If the field is a reference to an object:
      • Do not expose a method that modifies the internal field value.
      • If it is returned externally, return a copied object.

Discussion

Sign in with GitHub to comment. Discussions are stored as GitHub Issues.View on GitHub