Why can't this be used when calling the static interface method from the default interface method?

Why , when in the interface , calling the static interface method from the default interface method, I can not use this.staticInterfaceMethod(), and when in the normal class , it is perfectly acceptable to use the call of the static class method from the instance method this.staticClassMethod()(although this is bad style)?

At the same time, it is perfectly acceptable to use thiswithin the framework of standard interface methods - I can legally do the following:

interface I {   
    int MY_CONST = 7;   
    static void st_f() {}       
    default void f1() {}

    default void f_demo() {
        this.f1();                  // fine!
        int locvar = this.MY_CONST; // also fine! 

        this.st_f(); // c.ERR:  static method of interface I can only be accessed as I.st_f 
    }
}
+4
source share
1 answer

"The static method can only be called on an interface class."

, (§8.4.8). this , this.staticInterfaceMethod() , - , . ; , . , this.staticInterfaceMethod() , .

, , :

public interface InterfaceA {
    public static void staticInterfaceMethod() {
        ...
    }
}

public interface InterfaceB {
    public static void staticInterfaceMethod() {
        ...
    }
}

public class ClassAB implements InterfaceA, InterfaceB {
    ...
}

ClassAB? , , ; .

.

this , .. , , , , .

+1

Source: https://habr.com/ru/post/1693044/


All Articles