Effective Java Item 15: Minimise mutability

  1. Don’t provide setters(mutators)
  2. Make sure the class cannot be extended by making the final, or has a private constructor(one class cannot extend another class that has no default constructor, when you have a private constructor, you can have a static public function to build that object)
  3. Make all fields final (This rule can be broke sometimes. For example, because the hash value of a immutable object will never change, you may want to cache the hash value after the first time the hashCode() function is invoked)
  4. Make all fields private
  5. Ensure exclusive access to any mutable components (I don’t get what does this means)

--

--

Android developer and snowboarder base in Vancouver

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Rufus Zhu

Rufus Zhu

Android developer and snowboarder base in Vancouver