[External] : Re: Minor improvement to anonymous classes

Brian Goetz brian.goetz at oracle.com
Mon Aug 2 19:26:04 UTC 2021


I am happy to see this move forward as an ordinary RFE, though it would 
be good to do some corpus searches for `this$0` to gauge how broadly the 
assumption that inner classes always have this field is.

On 8/2/2021 1:34 PM, Liam Miller-Cushon wrote:
> On Sat, Jul 31, 2021 at 12:51 PM Brian Goetz <brian.goetz at oracle.com 
> <mailto:brian.goetz at oracle.com>> wrote:
>
>     > Another option that gets some of the benefit and doesn't require a
>     > spec change is to omit the reference to the enclosing instance
>     if it
>     > is unused. We've been doing that successfully for a few years at
>     > Google. There was some minor compatibility impact to code using
>     > reflection to access the synthetic field that stores the
>     reference to
>     > the enclosing instance, but that was very rare, and the benefit is
>     > that it avoids some memory leaks.
>
>     The people who would complain are in a pretty weak position:
>
>       - The field is private and synthetic; you shouldn't be
>     reflecting on
>     it at all;
>       - The field name is an implementation detail of the compiler,
>     and not
>     specified, so you shouldn't even pretend you know its name;
>       - If its your class, the easy thing to do is add an accessor, so
>     the
>     only people who would complain are those that are breaking into
>     classes
>     they don't control.
>
>     I think it is reasonable for the language to infer whether the
>     enclosing
>     instance is "captured" by the inner class or not, and that's
>     consistent
>     with how lambdas behave.
>
>
> Thanks, that all sounds right to me.
>
> I filed https://bugs.openjdk.java.net/browse/JDK-8271623 
> <https://bugs.openjdk.java.net/browse/JDK-8271623> to track that 
> enhancement.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.java.net/pipermail/amber-spec-experts/attachments/20210802/87d5b3fa/attachment-0001.htm>


More information about the amber-spec-experts mailing list