Sobes.tech
Middle

How to call a static method of an interface?

sobes.tech AI

Answer from AI

Static methods in interfaces, introduced in Java 8, are called directly by the interface name.

interface MyInterface {
    static void staticMethod() {
        System.out.println("Interface static method called.");
    }
}

class MyClass implements MyInterface {
    // The class can use the static method of the interface,
    // but cannot override it
}

public class Main {
    public static void main(String[] args) {
        // Direct call of static method via interface name
        MyInterface.staticMethod(); 
        
        // Cannot call static method via an object of the class
        // implementing the interface
        // MyClass obj = new MyClass();
        // obj.staticMethod(); // Compilation error
    }
}
How to call a static method of an interface? — Java - sobes.tech