Draft Spec for Flexible Constructor Bodies (JEP 513)

Remi Forax forax at univ-mlv.fr
Tue Apr 22 20:59:12 UTC 2025


----- Original Message -----
> From: "Stephan Herrmann" <stephan.herrmann at berlin.de>
> To: "amber-spec-experts" <amber-spec-experts at openjdk.org>
> Sent: Tuesday, April 22, 2025 7:59:33 PM
> Subject: Re: Draft Spec for Flexible Constructor Bodies (JEP 513)

> Am 22.04.25 um 16:59 schrieb Remi Forax:
>> Hello,
>> i'm not sure it's wise to have flexible constructor bodies to be out of preview.
> 
> Looking from a distance those seem to fit hand-in-glove, no?
> 
>> There is a non trivial interaction with the introduction of strict fields and i
>> am not sure those two should be separated, mostly because with strict fields,
>> initializing a field before the call to super() change its semantics
> 
> I scanned https://openjdk.org/jeps/8350458 but could not find anything that
> looked like changing a field's semantics based on where the field is
> initialized. It looks more like strictly initialized fields _require_
> initialization during "early larval state", no? So you need JEP 513 to work with
> strictly initialized fields in Java source code, right?

yes, it's the other way around,
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.


> 
> Stephan

Rémi

> 
>> while with
>> a flexible constructor body, one can already initialize the field before the
>> call to super() without the semantics being changed.
>> 
>> But i'm sure i'm missing something ...
>> 
>> regards,
>> Rémi
>> 
>> --------------------------------------------------------------------------------
>> 
>>     *From: *"Gavin Bierman" <gavin.bierman at oracle.com>
>>     *To: *"amber-spec-experts" <amber-spec-experts at openjdk.org>
>>     *Sent: *Tuesday, April 22, 2025 3:48:31 PM
>>     *Subject: *Draft Spec for Flexible Constructor Bodies (JEP 513)
>> 
>>     Dear experts,
>> 
>>     The first draft of a spec covering JEP 513 (Flexible Constructor Bodies) is
>>     available at:
>> 
>>     https://cr.openjdk.org/~gbierman/jep513/latest/ <https://cr.openjdk.org/
>>     ~gbierman/jep513/latest/>
>> 
>>     Feel free to contact me directly or on this list with any comments/corrections.
>> 
>>     Thanks,
>>     Gavin
>> 
>>         On 22 Apr 2025, at 13:02, Mark Reinhold <mark.reinhold at oracle.com> wrote:
>> 
>>         https://openjdk.org/jeps/513
>> 
>>           Summary: In the body of a constructor, allow statements to appear
>>           before an explicit constructor invocation, i.e., super(...) or
>>           this(...).  Such statements cannot reference the object under
>>           construction, but they can initialize its fields and perform other safe
>>           computations.  This change allows many constructors to be expressed
>>           more naturally.  It also allows fields to be initialized before they
>>           become visible to other code in the class, such as methods called from
>>           a superclass constructor, thereby improving safety.
>> 
>>         - Mark
>> 
>> 


More information about the amber-spec-observers mailing list