Java ProgramsOOPStatic Interface Method

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. 1static double applyDiscount(...) is defined directly inside interface Payment, with a full method body.
  2. 2It's called as Payment.applyDiscount(200.0, 10) — through the interface name itself, exactly like a static method on a class.
  3. 3CardPayment still implements the interface's abstract process method normally, and is used through a Payment reference like any other instance method.
  4. 4The static method is never inherited by CardPayment or callable through a Payment instance — 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

Related Programs