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

Add a warning when accessing a static method via an reference

    XMLWordPrintable

Details

    • Enhancement
    • Resolution: Fixed
    • P5
    • 7
    • 1.4.1, 5.0
    • tools
    • b86
    • generic, x86
    • generic, windows_xp
    • Not verified

    Description

      Name: rl43681 Date: 06/17/2003


      A DESCRIPTION OF THE REQUEST :
      The compiler allows static methods to be accessed as if they are instance methods. This can be a source of confusion when reading code.

      It would be clearer if all calls to static methods had to be prefixed with the class name - but forcing this would break a lot of existing code.

      It would be nice if the compiler had an option to warn when a static method is called without the class name being prefixed.

      JUSTIFICATION :
        To enhance the readabiloty of code (feel free to argue that it'll make the code harder to read - but hey that is why it is an option).

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The compiler emit a warning (if the user specifies the flag) when a static method is called without being prefixed by the class name.

      ---------- BEGIN SOURCE ----------
      public class X {
      public static void main(final String[] argv) {
      X x = new X();
      x.foo(); // warning - reference not a class
      X.foo(); // OK
      }

      public static void foo() {}

      void bar() {
      foo(); // warning - no class prefix
      X.foo(); // OK
      }
      }
      ---------- END SOURCE ----------
      (Review ID: 185711)
      ======================================================================

      Attachments

        Issue Links

          Activity

            People

              jjg Jonathan Gibbons
              rlewis Roger Lewis (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: