When the -Xlint:static option is enabled, javac will warn when a static method of a class is invoked via an instance of that class.
As of Java 8, interfaces can have static methods. Static methods on interfaces are not "inherited" the same way static methods on classes are. It is illegal to invoke such a static method via an instance of that interface, via a class that implements that interface, or via a sub-interface. (These are errors, not warnings.)
Unfortunately, there does not appear to be a warning issued for a long-standing (mis)feature of the Java language, which is that it is legal to invoke a static method of a class via a subclass. This is generally regarded as poor coding style. However, there is no warning issued for this case:
class A {
static void foo() { }
}
class B extends A { }
B.foo(); // no warning
It would be nice if a new lint option were added to issue warnings in this case.
I think a new lint option, separate from -Xlint:static, would be preferable to augmenting the scope of -Xlint:static, because code bases that are currently warning-free (like the JDK) would suddenly produce warnings when switching to a newer compiler. Having a separate lint option would make the transition easier to manage.
As of Java 8, interfaces can have static methods. Static methods on interfaces are not "inherited" the same way static methods on classes are. It is illegal to invoke such a static method via an instance of that interface, via a class that implements that interface, or via a sub-interface. (These are errors, not warnings.)
Unfortunately, there does not appear to be a warning issued for a long-standing (mis)feature of the Java language, which is that it is legal to invoke a static method of a class via a subclass. This is generally regarded as poor coding style. However, there is no warning issued for this case:
class A {
static void foo() { }
}
class B extends A { }
B.foo(); // no warning
It would be nice if a new lint option were added to issue warnings in this case.
I think a new lint option, separate from -Xlint:static, would be preferable to augmenting the scope of -Xlint:static, because code bases that are currently warning-free (like the JDK) would suddenly produce warnings when switching to a newer compiler. Having a separate lint option would make the transition easier to manage.
- relates to
-
JDK-4593045 warnings desired for almost certain errors
-
- Open
-