Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warnings for "possible this escape" similar to javac #3313

Open
datho7561 opened this issue Nov 14, 2024 · 0 comments
Open

Warnings for "possible this escape" similar to javac #3313

datho7561 opened this issue Nov 14, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@datho7561
Copy link
Contributor

Consider the following class:

public class HelloWorld {

	public static void main(String... args) {
		System.out.println(new Rectangle(4, 3));
	}

	public static class Rectangle {
		int width, height;
		transient int area = -1;
		public Rectangle (int width, int height) {
			this.width = width;
			this.height = height;
			this.precalculateArea();
		}
		public void precalculateArea() {
			this.area = width * height;
		}
		@Override
		public String toString() {
			return "area: " + area;
		}
	}
}

When compiling with javac -Xlint:all HelloWorld.java, javac produces the following warning:

HelloWorld.java:13: warning: [this-escape] possible 'this' escape before subclass is fully initialized
                        this.precalculateArea();
                                             ^
1 warning

My understanding is that this check is important for the upcoming value classes JEP. I think it would be nice if JDT also reported this warning.

@jukzi jukzi added the enhancement New feature or request label Nov 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants