Instance qualifier for static access warning and anonymous classes
Tagir Valeev
amaembo at gmail.com
Fri Apr 29 14:54:10 UTC 2022
Hello!
Consider the following code:
public class Demo {
public static void main(String[] args) {
var obj = new Object() {
static void foo() {
System.out.println("Static method of anonymous class");
}
};
obj.foo(); // Cannot replace instance qualifier with class reference
}
}
With -Xlint:static, javac reports the following warning:
Demo.java:8: warning: [static] static method should be qualified by
type name, <anonymous Demo$1>, instead of by an expression
obj.foo(); // Cannot replace instance qualifier with class reference
^
1 warning
It's questionable whether this warning should be reported at all, as
it's impossible to call the static method of anonymous class with
class name qualifier. But in any case, suggesting to use `<anonymous
Demo$1>` as a qualifier is invalid.
With best regards,
Tagir Valeev.
More information about the compiler-dev
mailing list