this-escape

Stephan Herrmann stephan.herrmann at berlin.de
Tue Apr 22 18:57:53 UTC 2025


Just now and by chance I found https://bugs.openjdk.org/browse/JDK-8015831

I tried to provoke this warning in the most obvious way I could think of:

public class Y {
	Y() {
		m();
	}
	void m() {}
}

public class X extends Y {
	String s = "Hello";
	@Override void m() {
		System.out.print(s);
	}
	public static void main(String... args) {
		System.out.print(new X().s);
	}
}

Much to my surprise javac -Xlint:all keeps quiet (all recent versions).

Running java X of course demonstrates that s is read before its initialization, 
exactly for that reason that Y() invokes an overridable method.

Where can I learn about the rules behind this validation?

thanks,
Stephan





More information about the compiler-dev mailing list