Oracle certified JPA Developer (1Z0-898),Oracle certified Java 8 Programmer I (1Z0-808), Oracle Java Web Service Developer (1z0-897), Oracle certified Java 7 Programmer, SCJA 1.0, SCJP 5.0, SCWCD 5.0, Oracle SQL Fundamentals I, CIW Certified Ecommerce specialist
Oracle certified JPA Developer (1Z0-898),Oracle certified Java 8 Programmer I (1Z0-808), Oracle Java Web Service Developer (1z0-897), Oracle certified Java 7 Programmer, SCJA 1.0, SCJP 5.0, SCWCD 5.0, Oracle SQL Fundamentals I, CIW Certified Ecommerce specialist
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:
Oracle certified JPA Developer (1Z0-898),Oracle certified Java 8 Programmer I (1Z0-808), Oracle Java Web Service Developer (1z0-897), Oracle certified Java 7 Programmer, SCJA 1.0, SCJP 5.0, SCWCD 5.0, Oracle SQL Fundamentals I, CIW Certified Ecommerce specialist
It's usually Rob Spoor who gets an answer in quickly like thatAbhay 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.)