Can an interface in Java contain static methods, and if so, how can they be used?

Yes, interfaces in Java can contain static methods. This feature was introduced in Java 8 to allow utility or helper methods to be placed directly inside an interface. Static methods belong to the interface itself and can be called without creating an object of the implementing class.

Key Points: • Static methods in an interface belong to the interface, not to its implementing classes. • They are invoked using the interface name. • Static methods cannot be overridden by implementing classes. • They are commonly used for utility, validation, or factory-related operations. • Static methods help keep interface-related functionality in a single place.

Example: A Payment interface may contain a static utility method to validate a transaction amount before processing payments.

Code Example:

interface Payment {

    void pay(double amount);

    static boolean isValidAmount(double amount) {
        return amount > 0;
    }
}

class UpiPayment implements Payment {

    @Override
    public void pay(double amount) {
        System.out.println("Payment Successful");
    }
}

public class Demo {

    public static void main(String[] args) {

        boolean valid =
                Payment.isValidAmount(1000);

        System.out.println(valid);
    }
}

Output:

true

Important Notes:

• Static methods are called using the interface name.

Example:

Payment.isValidAmount(1000);

• They cannot be called through an object reference.

Incorrect:

Payment payment = new UpiPayment(); payment.isValidAmount(1000); // Compilation Error

Common Uses:

• Validation methods • Utility/helper methods • Factory methods • Common business logic related to the interface

Interview Tip: A concise interview answer is:

"Yes, interfaces can contain static methods since Java 8. These methods belong to the interface itself and are called using the interface name. They are commonly used for utility or helper functionality and cannot be overridden by implementing classes."