[External] : Re: Minor improvement to anonymous classes
Brian Goetz
brian.goetz at oracle.com
Sat Jul 31 19:51:15 UTC 2021
> 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.
More information about the amber-spec-experts
mailing list