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
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