9

Java-8 allows define static methods inside interface, but restricts it invocation by only interface name:

9.4: An interface can declare static methods, which are invoked without reference to a particular object.

E.g.:

interface X {
    static void y() {
    }
}

...

X x = new X() {};
x.y();

causes error:

error: illegal static interface method call
        x.y();
            ^
  the receiver expression should be replaced with the type qualifier 'X'

Often in JLS such kind of prohibitions have an explanation. In this case I didn't found anything detailed. So I'm looking for a comprehensive or authoritative explanation of this rule: why it is prohibited to invoke static method via particular object reference? What does it break?

Andremoniy
  • 34,031
  • 20
  • 135
  • 241
  • 14
    Maybe the better question is why static methods can be invoked on object references in the first place... – Yosef Weiner Jan 10 '16 at 18:21
  • 1
    On that note, how are we getting an instance of an interface X on the line above? – aiguy Jan 10 '16 at 18:25
  • 2
    totally agree with @SkinnyJ I think Java developer are trying to correct their mistake that they did by making the the static avalaible by object which is somewhat confusing – singhakash Jan 10 '16 at 18:25
  • @aiguy by creating an anonymous class which implements that interface. – Tom Jan 10 '16 at 18:25
  • 1
    @SkinnyJ Very good question, I've been wondering that for the past year. I wonder if that question would be fit for one of the StackExchange sites – Vince Jan 10 '16 at 18:28
  • @VinceEmigh Maybe on [programmers SE](http://programmers.stackexchange.com/). – Tom Jan 10 '16 at 18:30
  • 2
    @VinceEmigh http://stackoverflow.com/questions/610458/why-isnt-calling-a-static-method-by-way-of-an-instance-an-error-for-the-java-co – Yosef Weiner Jan 10 '16 at 18:32
  • @Tom youre right. Didnt notice {} on first read. – aiguy Jan 10 '16 at 18:44
  • actually just tried doing this. the super interface's static method is not even available through reflections... how can you even invoke it? – aiguy Jan 10 '16 at 18:46

1 Answers1

18

It's a fairly strong consensus that the syntax in question shouldn't have been allowed for static methods on classes, either, but by the time that was realized it was too late to change. It wasn't too late for the recently added interface methods.

Additionally, permitting this syntax would introduce the possibility of the diamond problem, as a class could implement interfaces defining colliding methods.

chrylis -cautiouslyoptimistic-
  • 75,269
  • 21
  • 115
  • 152
  • 10
    This analysis is correct. We consider the ability to invoke a static method through an instance to be a language design error, unfortunately one which we can't fix retroactively for classes. We could at least not make the same mistake. (Sometimes we do choose to deliberately "make the same mistake" when extending the langauge, choosing consistency over local improvement -- but the choice involves a judgment call of "well, how much better is the improvement?" Here, the difference was large enough.) – Brian Goetz Jan 10 '16 at 22:42
  • 1
    @Brian Goetz: ...and it could break existing code (once recompiled), if a new `static` interface method turns out to be more specific than the formerly invoked instance method. I suppose, this is also (one of) the reason(s) for not inheriting `static` interface methods. The evolution of interfaces should have the least impact on existing implementations possible. – Holger Jan 11 '16 at 10:21
  • 1
    @Holger ...and further, introducing defaults and inheritable statics at the same time would have caused even more confusion. Lots of reasons to draw this line here. – Brian Goetz Jan 11 '16 at 16:05