Jesper de Jong wrote:Please UseCodeTags when you post source code.
A subclass cannot make your class mutable. But the subclass itself can be mutable. That could lead to strange things. For example:
Because Something is a subclass of Immutable, you can assign a Something object to a variable of type Immutable.
It's a good idea to make immutable classes final, so that you can be sure that a variable of type Immutable always refers to an object that is really immutable.
Also, make the member variables of your immutable class final:
It's usually Rob Spoor who gets an answer in quickly like that
Abhay Agarwal wrote: . . . I think I need to increase my typing speed
Can't you use mutable fields if there is no reference to them held elsewhere? So any references to those fields should be defensive copies, and defensive copies should be used in the constructor. And once they are in the immutable class, no attempt is ever made to change their state.
Mike Simmons wrote: . . . all the member fields must be immutable types as well.)