0

Section 3.4 of Java Concurrency in Practice says that an object is immutable if:

  1. Its state cannot be modified after construction;
  2. All its fields are final; and
  3. It is properly constructed (the this reference does not escape during construction)

Why does allowing the this reference to escape during construction prevent an object from being immutable?

Note that I understand there are many problems associated with allowing the this reference to escape during construction. My particular question here is why it prevents an object from being considered immutable.

Timothy McCarthy
  • 857
  • 13
  • 19
  • The question title asks why it stops a "class" from being immutable. Of course I mean an object. – Timothy McCarthy Jun 03 '16 at 01:16
  • 5
    improper construction means someone can see it in unfinished state, and again later in finished state. Immutable objects should only be observable in 1 state. – zapl Jun 03 '16 at 01:17
  • That seems like a good answer, but I can't accept it as a comment. I don't think this question has been asked before. Can you provide it as an answer? – Timothy McCarthy Jun 03 '16 at 01:21

0 Answers0