posted 15 years ago
Yes, that is the standard method for new values for immutable classes. Simple exampleNote:
Use final modifier to prevent anybody creating mutable subclasses.All "real" fields are final too.No set methods.If there were any mutable reference type fields, they would be copied before being returned by a get method. You could do that with a copy constructor in the field . . . return newFoo(this.foo);No need for copy constructors in immutable objectsThe hash code (or the return value for toString) can be calculated when required, or at construction time, and cached, if you prefer.