Flexible constructors: dubious error
Olexandr Rotan
rotanolexandr842 at gmail.com
Tue Oct 29 13:11:08 UTC 2024
Local classes, as well as inner ones (don't confuse with nested) implicitly
take parents "this" as a constructor argument (if I'm not mistaken, untill
some point of time or even now inner classes were elevated to top level
classes with reference to parent as their constructor argument).
In fact, if I remember correctly, the behaviour in version 23 is a known
bug that was fixed a while ago
On Tue, Oct 29, 2024, 15:02 Stephan Herrmann <stephan.herrmann at berlin.de>
wrote:
> Given:
>
> public class X {
> public static void main(String[] argv) {
> class Inner {
> String s;
> Inner() {
> class Local {}
> new Local() {};
> super();
> }
> Inner(int i) {
> class Local {}
> new Local() {
> void m() {
> System.out.println(s);
> }
> };
> super();
> }
> }
> new Inner();
> }
> }
>
> javac 23 reports exactly one error against the illegal use of 's' in
> Inner(int).
> Good.
>
> javac 24 ea+21 additionally flags both instantiations "new Local() ...":
>
> X.java:7: error: cannot reference this before supertype constructor has
> been called
> new Local() {};
> ^
>
> I don't see any reason for that error as I don't see any access to 'this'.
> What
> is the reason for this change in behavior?
>
> thanks,
> Stephan
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/compiler-dev/attachments/20241029/fffdb3ef/attachment.htm>
More information about the compiler-dev
mailing list