I am looking for some efficient way for building a immutable class, just like Java’s String class.
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.
privateand preferablyfinaloverridden – make the class final,
or use static factories and keep
constructors private
Constructor/Factory
fields
Collections.unmodifiable*. Also, collections should contain only immutable Objectsimmutable objects or use defensive copying
change the internal state of the
Object.
Tom Hawtin pointed out that
finalcan be optional.Stringclasshas a cachehashvar that is only assigned when the hash function is called.