Receiver parameter of local class constructor
Alex Buckley
alex.buckley at oracle.com
Fri Nov 22 12:04:28 PST 2013
I have filed https://bugs.openjdk.java.net/browse/JDK-8029042.
FYI I discovered this problem when attempting to write a local class
inside a default method of an interface. There is no reason why this
shouldn't work:
interface I {
default void m() {
class Inner {
Inner(I I.this) {}
}
}
}
The lexically enclosing type declaration of Inner is I, and the
qualified this expression "I.this" is legal in the body of I#m. The
receiver parameter shown above is perfectly natural in Java SE 8.
Alex
On 11/21/2013 5:04 PM, Alex Buckley wrote:
> If an inner class is a member, then a receiver parameter works fine:
>
> class Top {
> class Inner {
> Inner(Top Top.this) {}
> }
> }
>
> but if the inner class is local:
>
> class Top {
> void m() {
> class Inner {
> Inner(Top Top.this) {}
> }
> }
> }
>
> then javac (JDK8 b116) gets confused - it thinks Inner is top level:
>
> error: receiver parameter not applicable for constructor of top-level class
> Inner(Top Top.this) {}
> ^
>
> This is a bug. A local class is a perfectly good inner class - the ctor
> of Top$1Inner.class even takes a Top - so its constructor ought to
> permit a receiver parameter.
>
> Alex
More information about the type-annotations-dev
mailing list