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

Static method called via instance does not behave as specified

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Won't Fix
    • Icon: P4 P4
    • None
    • 1.1.5
    • tools
    • x86
    • windows_nt



      Name: tb29552 Date: 09/11/98


      /* The problem is that an expression of the form
       *
       * ClassName.staticFieldName.staticMethodName()
       *
       * is (sometimes?) not executed correctly.
       *
       *The problem can be reproduced with the following code:
       */
      public class StaticBug
      {
          public
              StaticBug()
              {
                  System.err.println("Constructing an instance of class StaticBug");
              }
          
          public static void
              main(String[] args)
              {
                  SomeClass.someStaticField.someStaticMethod();
              
                  // Everything goes allright when the following two
                  // lines are used instead:
                  //
                  //StaticBug b = SomeClass.someStaticField;
                  //b.someStaticMethod();
              }
          
          public static void
              someStaticMethod()
              {
                  System.err.println("In StaticBug.someStaticMethod()");
              }
      }

      class SomeClass
      {
          static
              {
                  System.err.println("Initializing class SomeClass");
              }
              
          public static StaticBug someStaticField = new StaticBug();
      }
      /*
       *
       * The output should be:
       *
       * Initializing class SomeClass
       * Constructing an instance of class StaticBug
       * In StaticBug.someStaticMethod()
       *
       * but it is:
       *
       * In StaticBug.someStaticMethod()
       *
       * So class SomeClass is not initialized.
       *
       * According to the JLS (First printing, August 1996)
       * section 15.11.4.1, the expression
       * 'SomeClass.someStaticField' should be "evaluated, but
       * the result is then discarded." To me, this implies
       * that SomeClass is loaded and initialized, and that an
       * instance of StaticBug is created.
       *
       * As shown above, if the single statement is split
       * into two parts, everything goes as specified.
       *
       * I marked this bug as a compiler bug, but I'm not
       * sure. Since all Java compiler/VM combinations I've
       * tried behave the same, it may be a VM or VM spec bug.
       */

      (Review ID: 37734)
      ======================================================================

            dstoutamsunw David Stoutamire (Inactive)
            tbell Tim Bell
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: