Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags


How to write an interface with the static method in Java

Sumit Sharma

In this shot, we will discuss how to write an interface with static methods.

Java 8 came with many new features, including interfaces with static methods.

Static methods in interfaces

A static method in an interface is the same as defined inside a class. Moreover, static methods can be called within other static and default methods.


Let us look at the below code snippet:

interface Car {
   //static interface methods
   static int getHP(int rpm, int torque) {
       return (rpm * torque) / 5252;

class myMain1 
   public static void main(String[] args) {
       System.out.println("alto car has a horsepower of: "+Car.getHP(3500,69));
Implementing static method in Java


  • In lines 1 to 6, we make an interface using a static method with a body.

  • In lines 8 to 13, we see an interface’s static method is called without the interface’s implementation or the creation of any object.

Now, if we want to calculate the horsepower of a given car’s engine, we call the getHP() method:

Car.getHP(2500, 480));

Static interface methods were introduced to provide a simple mechanism that increases a design’s degree of cohesion by putting together related methods in one single place without having to create an object.



View all Courses

Keep Exploring