Java - Create Immutable Class

How to Create Immutable Class in Java ?
 To create immutable class we need to follow certain steps,

  • Declare the class as Final
  • Make all fields as Private & Final  
  • Don't Create Setter for any fields
  • Initialize all fields via constructor
  • Perform cloning of objects in the getter methods to return a copy rather than returning the actual object reference.

Comments

Popular posts from this blog

How to set Java Object into JaxBElement ?

GitLab