JDK-8300786 - No longer require super() and this() to appear first in a constructor
Brian Goetz
brian.goetz at oracle.com
Tue Feb 7 22:21:26 UTC 2023
I think the approach is right, the specific list of stuff might need to
be reviewed more carefully (specifically, I've not thought through the
connection with inner classes, which we've now opened the door to.)
Let's call it a placeholder and we'll make sure to review it more
carefully after you've worked through the implementation and caught any
obvious gaps?
On 2/7/2023 12:52 PM, Archie Cobbs wrote:
> 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-experts/attachments/20230207/dde91771/attachment-0001.htm>
More information about the amber-spec-experts
mailing list