Draft Spec for Flexible Constructor Bodies (JEP 513)

John Rose john.r.rose at oracle.com
Tue Apr 22 21:15:59 UTC 2025


On 22 Apr 2025, at 13:59, Remi Forax wrote:

> if you take a look to a code, the same code with strict field and without strict fields do not offer the same publication guarantee.
>
>>
>> Where should I be looking for that property of strictly initialized fields you
>> are referring to?
>
> Let's take an example,
>
> class Foo {
>   int x;
>
>   Foo(int x) {
>     this.x = x;
>     super();
>   }
> }
>
> // thread 1
> global = new Foo(3);
>
> // thread 2
> var foo = global;
> if (foo != null) {
>   System.out.println(foo.x);
> }
>
> the question is what are the values that can be printed, with flexible constructor bodies, it's 0 or 3, with strict fields, it's only 3.
>
> So you have no way to understand the behavior of the code just by looking at it.

That’s fine, but any change in behavior would be due to strict
fields, as a feature. The behavioral difference you point out
(0 vs. 3) is in the noise, since either outcome is valid in
the JMM, under current rules (or with no strict fields present).

Note that SF (strict fields) add more determinism. That is not
a flaw in layers of the language added previously, including
FCB (flexible constructor bodies).  The new layer (SF) will be
a improvement over something that was already good.

Overall, I don’t see any reason we can’t add this in two layers,
FCB first, and then SF.  And I see plenty of reasons to do this.
The main reason to split the work is the fact that SF requires
deep cuts to the VM, while FCB requires no VM changes at all.

— John

P.S. The reason strict fields would make the above code
more deterministic is that the presence of a strict field
causes the VM to add a mid-construction fence, after the
strict field is set and before /this/ becomes accessible.
Thus, 0 becomes impossible.  This mid-construction fence
is the same kind of operation as the post-construction
fence which the JMM calls the “freeze” operation.  The
basic effect of strict fields is to maneuver a JMM
freeze operation into the gap between the last
initialization of a strict field and the first point
at which /this/ might become visible.  Certain
new structural rules that the VM enforces for strict
fields will ensure that there IS such a gap present.
None of that is required for FCB.



More information about the amber-spec-observers mailing list