Unexpected behavior when changing the modifier of a member variable in connection with an inner class

Jesper Steen Møller jesper at selskabet.org
Tue May 5 15:19:41 UTC 2020


Hi Lars

I'm not sure we're entirely on topic here, but I couldn't resist:

Are you preparing a puzzler talk, or writing a compiler or other semantic tool? ;-)

The detail is in https://docs.oracle.com/javase/specs/jls/se14/html/jls-8.html#jls-8.2 <https://docs.oracle.com/javase/specs/jls/se14/html/jls-8.html#jls-8.2>.

In "Inner", only one x is visible, namely Outer.this.x. The private x is not inherited into Inner, as per §8.2 "Members of a class that are declared private are not inherited by subclasses of that class."

In "Inner", both y's are visible and this.y is the closest one, i.e. the one inherited from Outer, qualified as Outer.Inner.this.y. You can still access the other y, by using Outer.this.y.

Hope this "helps".

-Jesper

BTW, IMHO: While name inhertance and scope rules are tricky for members, they're mind-boggling for type names, at least for my mind.




More information about the discuss mailing list