As the title, I am reading <Effective Java>, but when i read chapter2, it says
As the title, I am reading <Effective Java> , but when i read chapter2,
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
When objects are immutable, you have much less to worry about than when objects (like JavaBeans) are mutable. Once you have an immutable object it is basically immune to thread synchronization problems, and you can freely pass it around without making defensive copies (to protect users or well-intentioned library methods from changing the data).
Naturally, creating a mutable object can be tricky, and may involve constructors far too long to manage. This is where the Builder pattern can help you–use a mutable Builder to create the initial state, then copy it to an immutable object in that object’s constructor.
Note that if the immutable object contains references to any mutable objects, the outside object isn’t really immutable. You may find
ImmutableList,ImmutableMap, andImmutableSethandy in Guava.