When is an interface with a default method initialized?

This is a very interesting issue!

It seems like JLS section 12.4.1 ought to cover this definitively. However, the behavior of Oracle JDK and OpenJDK (javac and HotSpot) differs from what’s specified here. In particular, the Example 12.4.1-3 from this section covers interface initialization. The example as follows:

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

Its expected output is:

1
j=3
jj=4
3

and indeed I get the expected output. However, if a default method is added to interface I,

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

which clearly indicates that interface I is being initialized where it wasn’t before! The mere presence of the default method is enough to trigger the initialization. The default method doesn’t have to be called or overridden or even mentioned, nor does the presence of an abstract method trigger initialization.

My speculation is that the HotSpot implementation wanted to avoid adding class/interface initialization checking into the critical path of the invokevirtual call. Prior to Java 8 and default methods, invokevirtual could never end up executing code in an interface, so this didn’t arise. One might think this is part of the class/interface preparation stage (JLS 12.3.2) which initializes things like method tables. But perhaps this went too far and accidentally did full initialization instead.

I’ve raised this question on the OpenJDK compiler-dev mailing list. There’s been a reply from Alex Buckley (editor of the JLS) in which he raises more questions directed at the JVM and lambda implementation teams. He also notes that there’s a bug in the spec here where it says “T is a class and a static method declared by T is invoked” should also apply if T is an interface. So, it might be that there are both specification and HotSpot bugs here.

Disclosure: I work for Oracle on OpenJDK. If people think this gives me an unfair advantage at getting the bounty attached to this question, I’m willing to be flexible about it.

Leave a Comment