B-Mac
B-Mac

Reputation: 567

Java let me define a static method in an interface

public interface B {
    String iname = "TBA";
    int iid = 0;

    public static void main(String[] args) {
        System.out.println("Hello");
    }

    public static void goForIt() {
        System.out.println("Went for it");
    }

    public void doSomething();

}

And now...

public class D {

    public static void main(String[] args) {
        B.goForIt();
    }
}

This successfully printed "Went for it". I was told that interfaces cannot have static methods though. So, what's going on here? However, when I have a class implement B, then the static method doesn't work.

Upvotes: 4

Views: 81

Answers (1)

rgettman
rgettman

Reputation: 178343

Java 8 has introduced the ability to have static methods in interfaces.

Enhancements in Java SE 8

... In addition, you can define static methods in interfaces.

But static methods are still not inherited, either from implementing an interface or from a superclass.

Upvotes: 3

Related Questions