Flexible constructors: dubious error

Stephan Herrmann stephan.herrmann at berlin.de
Tue Oct 29 13:02:30 UTC 2024


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