Static Interface Method in Java
intermediate· OOP · Interfaces
Problem
Since Java 8, an interface can define a static method with a full method body, called directly on the interface name itself, never through an implementing class's instance.
Define a Payment interface with a static utility method, and call it directly on the interface.
Input
amount = 200.0, percent = 10
Output
Processing payment: 150.0
Discounted amount: 180.0
Java Program
Java
interface Payment {
void process(double amount);
static double applyDiscount(double amount, double percent) {
return amount - (amount * percent / 100);
}
}
class CardPayment implements Payment {
public void process(double amount) {
System.out.println("Processing payment: " + amount);
}
}
public class StaticInterfaceMethodDemo {
public static void main(String[] args) {
Payment p = new CardPayment();
p.process(150.0);
// Called through the interface name, not through p
double discounted = Payment.applyDiscount(200.0, 10);
System.out.println("Discounted amount: " + discounted);
}
}Output
Processing payment: 150.0
Discounted amount: 180.0
Core Logic
Attaching a static method directly to the interface keeps a utility calculation next to the contract it supports, callable without needing any implementing object at all.
How It Works
- 1
static double applyDiscount(...)is defined directly insideinterface Payment, with a full method body. - 2It's called as
Payment.applyDiscount(200.0, 10)— through the interface name itself, exactly like a static method on a class. - 3
CardPaymentstill implements the interface's abstractprocessmethod normally, and is used through aPaymentreference like any other instance method. - 4The static method is never inherited by
CardPaymentor callable through aPaymentinstance —p.applyDiscount(...)would not compile.
Payment.applyDiscount(200.0, 10) computes 200.0 - (200.0 * 10 / 100), returning 180.0.💡
Key Point: Static interface methods and default methods solve different problems — a default method adds shared instance behavior every implementer inherits, while a static method is a standalone utility that belongs conceptually to the interface but is never part of any implementing object.
Key Concepts
static interface methodinterfaceJava 8