-
Bug
-
Resolution: Duplicate
-
P4
-
None
-
8
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
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
- duplicates
-
JDK-8043188 12.4.1: Trigger interface initialization on default/static method invocation
- Closed