Home > Author > >

" An object is immutable if: Its state cannot be modifled after construction; All its flelds are final;[12] and [12] It is technically possible to have an immutable object without all fields being final—String is such a class—but this relies on delicate reasoning about benign data races that requires a deep understanding of the Java Memory Model. (For the curious: String lazily computes the hash code the first time hashCode is called and caches it in a nonfinal field, but this works only because that field can take on only one nondefault value that is the same every time it is computed because it is derived deterministically from immutable state. Don't try this at home.) It is properly constructed (the this reference does not escape during construction). "

, Java Concurrency in Practice


Image for Quotes

 quote : An object is immutable if: Its state cannot be modifled after construction; All its flelds are final;[12] and [12] It is technically possible to have an immutable object without all fields being final—String is such a class—but this relies on delicate reasoning about benign data races that requires a deep understanding of the Java Memory Model. (For the curious: String lazily computes the hash code the first time hashCode is called and caches it in a nonfinal field, but this works only because that field can take on only one nondefault value that is the same every time it is computed because it is derived deterministically from immutable state. Don't try this at home.) It is properly constructed (the this reference does not escape during construction).