Java Reference
In-Depth Information
17.5.1. Semantics of
final
Fields
Let
o
be an object, and
c
be a constructor for
o
in which a
final
field
f
is written. A
freeze
action on
final
field
f
of
o
takes place when
c
exits, either normally or abruptly.
Note that if one constructor invokes another constructor, and the invoked constructor sets a
final
field, the freeze for the
final
field takes place at the end of the invoked constructor.
For each execution, the behavior of reads is influenced by two additional partial orders,
the dereference chain
dereferences()
and the memory chain
mc()
, which are considered to
be part of the execution (and thus, fixed for any particular execution). These partial orders
must satisfy the following constraints (which need not have a unique solution):
• Dereference Chain: If an action
a
is a read or write of a field or element of an ob-
ject
o
by a thread
t
that did not initialize
o
, then there must exist some read
r
by
thread
t
that sees the address of
o
such that
r dereferences(r, a)
.
• Memory Chain: There are several constraints on the memory chain ordering:
♦ If
r
is a read that sees a write
w
, then it must be the case that
mc(w, r)
.
♦ If
r
and
a
are actions such that
dereferences(r, a)
, then it must be the case that
mc(r, a)
.
♦ If
w
is a write of the address of an object
o
by a thread
t
that did not initialize
o
, then there must exist some read
r
by thread
t
that sees the address of
o
such
that
mc(r, w)
.
Given a write
w
, a freeze
f
, an action
a
(that is not a read of a
final
field), a read
r
1
of the
final
field frozen by
f
, and a read
r
2
such that
hb(w, f)
,
hb(f, a)
,
mc(a, r
1
)
, and
dereferences(r
1
,
r
2
)
, then when determining which values can be seen by
r
2
, we consider
hb(w, r
2
)
. (This
happens-before
ordering does not transitively close with other
happens-before
orderings.)
Note that the
dereferences
order is reflexive, and
r
1
can be the same as
r
2
.
For reads of
final
fields, the only writes that are deemed to come before the read of the
final
field are the ones derived through the
final
field semantics.
17.5.2. Reading
final
Fields During Construction
A read of a
final
field of an object within the thread that constructs that object is ordered
with respect to the initialization of that field within the constructor by the usual
happens-
before
rules. If the read occurs after the field is set in the constructor, it sees the value the
final
field is assigned, otherwise it sees the default value.