Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Anchor
effectively immutable
effectively immutable

Wiki Markup
*effectively immutable* : When applied to an object, this means
that "its state cannot be seen to change by callers, which implies that

  • all public fields are final,
  • all public final reference fields refer to other immutable objects, and
  • constructors and methods do not publish references to any internal state which is potentially mutable by the implementation.

Wiki Markup
 that at least one of the conditions for immutability does not hold but the object's state cannot be changed after initialization or publication. "Effectively immutable objects may still have internal mutable state for purposes of performance optimization; some state variables may be lazily computed, so long as they are computed from immutable state and that callers cannot tell the difference." \[[Goetz 07|AA. Java References#Goetz 07]\]. "Using effectively immutable objects can simplify development and improve performance by reducing the need for synchronization." \[[Goetz 06|AA. Java References#Goetz 06]\]

Anchor
happens-before order
happens-before order

...

Wiki Markup
*heap memory* : "Memory that can be shared between threads is called shared memory or heap memory. All instance fields, static fields and array elements are stored in heap memory.\[...\] Local variables (§14.4), formal method parameters (§8.4.1) or exception handler parameters are never shared between threads and are unaffected by the memory model." \[[JLS 05|AA. Java References#JLS 05]\]. 

Anchor
immutable
immutable

immutable : When applied to an object, this means that its state cannot be changed after being initialized. "An object is immutable if:

  • Its state cannot be modified after construction;
  • Wiki Markup
    All its fields are final;\[12\] and
  • It is properly constructed (the this reference does not escape during construction).

Wiki Markup
\[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." \[[Goetz 06*immutable* : When applied to an object, this means that its state cannot be changed after being initialized. In addition to being [effectively immutable|BB. Definitions#effectively immutable], all fields must be final.
 Immutable objects are inherently thread-safe; they may be passed between threads or published without synchronization.  \[[Goetz 07|AA. Java References#Goetz 0706]\].

Immutable objects are inherently thread-safe; they may be passed between threads or published without synchronization.

Anchor
initialization safety
initialization safety

...