[External] : Re: Minor improvement to anonymous classes

Liam Miller-Cushon cushon at google.com
Mon Aug 2 17:34:07 UTC 2021


On Sat, Jul 31, 2021 at 12:51 PM Brian Goetz <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 to track that
enhancement.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.java.net/pipermail/amber-spec-experts/attachments/20210802/2f4a36d6/attachment.htm>


More information about the amber-spec-experts mailing list