<div dir="ltr"><div dir="ltr"><div>I think one thing we could do to help lower the confusion level is come up with some less ambiguous "enclosing" terminology.</div><div><br></div><div>For example, the term "enclosing instance" is ambiguous. Consider this class:</div><div><br><div style="margin-left:40px"><span style="font-family:monospace">$ cat E1.java</span></div><div style="margin-left:40px"><span style="font-family:monospace"><br></span></div><div style="margin-left:40px"><span style="font-family:monospace">class E1 {</span><br><span style="font-family:monospace">  E1() {</span><br><span style="font-family:monospace">    class E2 {</span><br><span style="font-family:monospace">      E2() {</span><br><span style="font-family:monospace">        class E3 {</span><br><span style="font-family:monospace">          void run() {</span><br><span style="font-family:monospace">            System.out.println(E1.this);    // ok</span><br><span style="font-family:monospace">          //System.out.println(E2.this);    // error</span><br><span style="font-family:monospace">            System.out.println(E3.this);    // ok</span><br><span style="font-family:monospace">          }</span><br><span style="font-family:monospace">        }</span><br><span style="font-family:monospace">        new E3().run();</span><br><span style="font-family:monospace">        super();</span><br><span style="font-family:monospace">      }</span><br><span style="font-family:monospace">    }</span><br><span style="font-family:monospace">    new E2();</span><br><span style="font-family:monospace">  }</span><br><span style="font-family:monospace"></span><br><span style="font-family:monospace">  public static void main(String[] args) {</span><br><span style="font-family:monospace">    new E1();</span><br><span style="font-family:monospace">  }</span><br><span style="font-family:monospace">}</span></div><div style="margin-left:40px"><span style="font-family:monospace"><br></span></div><div style="margin-left:40px"><span style="font-family:monospace">$ javac --enable-preview --release 24 E1.java && java --enable-preview -classpath classes E1<br>E1@6537cf78<br>E1$1E2$1E3@3cbbc1e0</span><br></div></div><div><br></div><div>Does class E3 have an "enclosing instance"?</div><div><br></div><div>It's a trick question....</div><div><ul><li>If "yes", then why is E2.this inaccessible?</li><li>If "no", then why is E1.this accessible?</li></ul></div><div>The compiler's "NOOUTERTHIS" doesn't help. It doesn't mean there's no outer 'this' instance, it just means there's no <i>immediate</i> outer 'this' instance.. and the "outerThisStack" is the stack of outer instances you get when you skip over the NOOUTERTHIS ones (I think).<br></div><div><br></div><div>Regardless, when looking at the compiler code, there's many mentions of "enclosing instances" and "outer instances" but it's easy to have the wrong assumption in your head about what these terms actually mean.<br></div><div><br></div><div>In an ideal world we would all agree to use some standard, non-ambiguous terminology, e.g.:<br></div><div><ul><li>The phrasing should always be "enclosing instance with respect to D", where D is some lexically enclosing class.</li><li>For a class C, the "enclosing instance of C with respect to D" is <i>defined</i> if:<br></li><ul><li>D is a class (not interface, record, enum, etc.)<br></li><li>C is nested within D</li><li>There is no static class (or interface, record, enum, etc.) declaration between C and D (including C itself)</li></ul><li>For a class C, the "enclosing instance of C with respect to D" is <i>accessible</i> at some point X in C if:</li><ul><li>The enclosing instance of C with respect to D is defined</li><li>X is not in an early construction context of D</li></ul><li>If C is nested directly within D with no intermediate classes, then "enclosing instance of C with respect to D" is also "the immediately enclosing instance of C".<br></li></ul></div></div><div>-Archie</div><div><br></div><div><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, Jun 7, 2024 at 5:13 PM Maurizio Cimadamore <<a href="mailto:maurizio.cimadamore@oracle.com" target="_blank">maurizio.cimadamore@oracle.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">On 02/06/2024 22:36, Stephan Herrmann wrote:<br>
> Do you consider the proposed rules too complex to implement cleanly, <br>
> or too complex to explain to users? <br>
<br>
It's a decent rule and explanation of what the compiler does.</blockquote></div><br><span class="gmail_signature_prefix">-- </span><br><div dir="ltr" class="gmail_signature">Archie L. Cobbs<br></div></div></div>