Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8139827

lint: javac should warn when a static method is called via a subclass

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Unresolved
    • Icon: P4 P4
    • tbd
    • None
    • tools
    • None

      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.

            Unassigned Unassigned
            smarks Stuart Marks
            Votes:
            2 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated: