Draft Spec for Second Preview of Flexible Constructor Bodies (JEP 482)

Maurizio Cimadamore maurizio.cimadamore at oracle.com
Mon Jun 10 20:36:59 UTC 2024


On 10/06/2024 19:21, Archie Cobbs wrote:
> In fact the compiler itself has a few examples, e.g., TypeEnter.java 
> line 301 - the constructor TypeEnter.ImportsPhase() invokes super(..., 
> new TypeEnter.HierarchyPhase()) which wouldn't be possible if early 
> construction contexts were treated as static (i.e., no access to 
> enclosing instances).

I get your "slippery slope argument".

The example you mention is like this:

```
class Outer {
    class A {
         A() { this(new B()); }
         ...
    }

    class B { ... }
}
```

Both A and B are member inner classes, and they have an enclosing 
instance of type Outer. This enclosing instance is what's used to create 
a new B() from within the constructor of A.

Note that in this case we're not really talking of a class declaration 
occurring in a pre-construction context, so we don't really have to 
decide anything when it comes to enclosing instances.

This example boils doing to deciding whether "Outer.this" is valid from 
the pre-construction context of A. And the answer is yes, because A has 
an enclosing instance of type Outer.

So, what I suggested (treat all class declarations in pre-construction 
context as having no enclosing instance) would really not change any of 
this.

But I suspect what you meant (but you didn't say) is that this would 
feel inconsistent - e.g. `new B()` is ok. But if you have a local class 
declaration and the declaration captures Outer.this, that would not be 
ok if the local decl is static.

If this is what you meant, I get it - although not too sure what to make 
of it. In the sense that it's not super clear whether these two examples 
should behave in exactly the same way (given one doesn't introduce a 
_new_ class declaration).

I guess for now I'd be very interested in learning how compilers (other 
than javac) deal with some of the cases we discussed in this thread (if 
they do at all, given javac's behavior is currently outside JLS).

Maurizio



More information about the amber-spec-experts mailing list