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

Eager initialization of a superinterface with a default method

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 9
    • 9
    • hotspot
    • None
    • b96

      Initialization of an interface triggers an initialization of a superinterface with a default method.
      It's agains JVMS. JVMS-5.5 allows triggering superinterface w/ default method initialization only by a dependent class, but not an interface:
      "5.5. Initialization
      A class or interface C may be initialized only as a result of:
      ...
      If C is an interface that declares a non-abstract, non-static method, the initialization of a class that implements C directly or indirectly.
      ..."

      Test case:
      public class IntfInit {
          interface I {
              int i = IntfInit.out("I::i", 1);
              default void method() { } // causes initialization!
          }
          interface J extends I {
              int j = IntfInit.out("J::j", 2);
          }
          interface K extends J {
              int k = IntfInit.out("K::k", 3);
          }

          public static void main(String[] args) {
              System.out.println(K.k);
          }

          static int out(String s, int i) {
              System.out.println(s + "=" + i);
              return i;
          }
      }

      Output:
      I::i=1
      K::k=3
      3

            acorn Karen Kinnear (Inactive)
            vlivanov Vladimir Ivanov
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: