How are Static Methods in interfaces different from Default Methods in Java 8?

Static methods and default methods are both allowed to carry a body in a Java 8 interface, but a static method belongs to the interface itself and is called on the interface name, while a default method belongs to implementing instances and is inherited by every class that implements the interface.

Key Points: • A static interface method is invoked as InterfaceName.methodName(), never on an instance, and it cannot be overridden by implementing classes. • A default method is invoked on an instance, like obj.methodName(), and implementing classes can optionally override it. • Static methods are typically used for utility or factory logic related to the interface, similar to how Comparator.comparing() is a static method on Comparator. • Default methods are used to add new behavior to an interface while preserving backward compatibility for existing implementers. • Neither static nor default methods allow interfaces to hold mutable instance state; both are limited to behavior, not fields.

Example: Comparator.naturalOrder() is a static method called directly on the Comparator interface, whereas Comparator.reversed() is a default method called on a specific comparator instance to flip its ordering.

Code Example:

interface MathUtils {
    static int square(int x) {
        return x * x;
    }

    default int cube(int x) {
        return x * x * x;
    }
}

// MathUtils.square(5) is called on the interface

Interview Tip: A concise interview answer is:

"Static methods on an interface belong to the interface itself and are called like InterfaceName.method(), and they can't be overridden. Default methods belong to instances, are called on an object, and can be overridden by implementing classes. Static methods are typically used for utility logic, like factory or helper methods, while default methods extend an interface's contract without breaking existing implementers."