JDK-8300786 - No longer require super() and this() to appear first in a constructor

Archie Cobbs archie.cobbs at gmail.com
Tue Feb 7 17:52:15 UTC 2023


Ping to Brian...

Any thoughts or comments on the proposed description below for
"pre-initialization context"?

I went ahead and added it into the JEP draft but am happy to adjust as
needed.

Thanks,
-Archie

On Thu, Feb 2, 2023 at 3:49 PM Archie Cobbs <archie.cobbs at gmail.com> wrote:

> On Thu, Feb 2, 2023 at 2:16 PM Brian Goetz <brian.goetz at oracle.com> wrote:
>
>> This can be amended to:
>>
>> An explicit constructor invocation statement introduces an
>> _pre-initialization context_, which limits the use of constructs that refer
>> to the current object.  Notably, the keywords `this` and `super` are
>> prohibited in a pre-initialization context, as are unqualified references
>> to instance variables and instance methods of lexically enclosing
>> declarations.
>>
>
> That might not work. For example, this source is legal (as it should be)
> and yet the 'this' keyword appears within the super() call:
>
> $ cat Outer.java
> public class Outer {
>
>     public int x;
>
>     public Outer(int x) {
>         this.x = x;
>     }
>
>     public class Inner extends Outer {
>         public Inner() {
>             super(Outer.this.x + 10);
>         }
>     }
>
>     public static void main(String[] args) {
>         System.out.println(new Outer(10).new Inner().x);
>     }
> }
> $ java -cp classes Outer
> 20
>
> What about something like this?
>
> An explicit constructor invocation statement introduces a *pre-initialization
>> context*, which includes the prologue of the constructor and the
>> explicit constructor invocation statement, and which prohibits the use of
>> constructs that refer explicitly or implicitly to the current object. These
>> include this or super referring to the current object, unqualified
>> references to instance variables or instance methods of the current object,
>> method references referring to instance methods of the current object, and
>> instantiations of inner classes of the current object's class for which the
>> current object is the enclosing instance (§8.1.3).
>>
>
-- 
Archie L. Cobbs
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-spec-observers/attachments/20230207/e5349e7a/attachment.htm>


More information about the amber-spec-observers mailing list