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

Initialization of interface with default method is missing from JLS8

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: P4
    • Resolution: Duplicate
    • Affects Version/s: 8
    • Fix Version/s: None
    • Component/s: specification
    • Labels:

      Description

      A DESCRIPTION OF THE PROBLEM :
      Interface initialization is covered by JLS 12.4.1 [1]. Example 12.4.1-3
      illustrates this:

           interface I {
               int i = 1, ii = Test.out("ii", 2);
           }
           interface J extends I {
               int j = Test.out("j", 3), jj = Test.out("jj", 4);
           }
           interface K extends J {
               int k = Test.out("k", 5);
           }
           class Test {
               public static void main(String[] args) {
                   System.out.println(J.i);
                   System.out.println(K.j);
               }
               static int out(String s, int i) {
                   System.out.println(s + "=" + i);
                   return i;
               }
           }

      The expected output is:

           1
           j=3
           jj=4
           3

      Indeed, I get the expected output for the example as it stands. However, if a
      default method is added to interface I, the behavior of the program changes.
      Consider:

           interface I {
               int i = 1, ii = Test.out("ii", 2);
               default void method() { } // causes initialization!
           }

      The output changes to:

           1
           ii=2
           j=3
           jj=4
           3

      That is, the "ii=2" line indicates that interface I is being initialized, that wasn't before. The presence of a default method seems to trigger the change, even if the default method is never called, referenced, or
      overridden.

      This behavior of interface is not mentioned in provided JLS document.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The JLS for the behavior of interface with default method should be there in doc.
      ACTUAL -
      NA
      Missing from documentation.

      URL OF FAULTY DOCUMENTATION :
      http://docs.oracle.com/javase/specs/jls/se8/html/jls-12.html#jls-12.4.1

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              abuckley Alex Buckley
              Reporter:
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: