Flexible constructors: dubious error

Stephan Herrmann stephan.herrmann at berlin.de
Tue Oct 29 14:10:47 UTC 2024


Am 29.10.24 um 14:11 schrieb Olexandr Rotan:
> Local classes, as well as inner ones (don't confuse with nested) implicitly take 
> parents "this" as a constructor argument

They *may* take this argument *if needed*.

But this is more an implementation detail and not the level at which the 
specification defines this, is it?

Stephan

> (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 
> <mailto: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
> 



More information about the compiler-dev mailing list