Possible ways of making an Object immutable
问题 I am looking for some efficient way for building a immutable class, just like Java's String class. 回答1: All the fields must be private and preferably final Ensure the class cannot be overridden - make the class final, or use static factories and keep constructors private Fields must be populated from the Constructor/Factory Don't provide any setters for the fields Watch out for collections. Use Collections.unmodifiable* . Also, collections should contain only immutable Objects All the getters