Static Methods in Java Interfaces

A guide to static methods in Java interfaces that demonstrates their features and benefits with practical examples.

Overview

In Java, instance methods are associated with an instance of a class. That means the method behaviour may be particular to the particular instance of that class. On the other hand, the static methods are associated with the class itself. Thus, their behaviours are not instance-specific, and we don’t need to instantiate the class to execute them.

We can also add static methods into Java Interfaces, including private static methods. This tutorial provides an in-depth overview of the static methods in Java Interfaces.

What is a Static Method in a Java Interface?

Since the beginning of Java, the interfaces only held public abstract methods. However, the Java 8 release allowed interfaces to have static and default methods. Both methods have a body – a complete set of instructions for their behaviour.

A static method in Java has a static modifier and a body.

public interface Calculator {
  static int add(int x, int y) {
    return x + y;
  }

  static int subtract(int x, int y) {
    return x - y;
  }
}Code language: Java (java)

As the static methods are class-level and not instance-level, we can use the static methods from an interface only by using the Interface’s name.

Calculator.add(10, 20);
Calculator.subtract(20, 3);Code language: Java (java)

We can use Interface’s static methods to encapsulate behaviours we don’t want sub-interfaces or sub-classes to override or inherit. Also, we can use the static methods to build reusable utilities that are not bound to any specific implementing classes or types.

Rules of Static Methods in a Java Interface

The default methods in interfaces, as we know, contain instructions for default behaviour, and an implementing class can choose to inherit it as is or provide a more specific instruction by overriding it.

Like default methods, the static methods have a body and a complete set of instructions for their behaviour. However, an implementing class cannot override or inherit a static method of its Interface.

Here are Java Interface’s static method rules that we should remember.

  1. All static methods must have the static modifier in their declaration.
  2. They must have a body.
  3. We can invoke the static methods by using the interface name only.
  4. No sub-interface or sub-class can override or inherit static methods.
  5. They cannot invoke any other default or abstract method.
  6. They are by default public, but we can make them private.

Private Static Methods in a Java Interface

Like any other method in a Java Interface, static methods are public by default. However, we can make them private if required.

private static void logMessage(String msg) {
  //Log it
}Code language: Java (java)

A private static method in an interface can only be accessed by other static methods in the same Interface.

public interface Calculator {
  static int add(int x, int y) {
    logMessage("calculating (" + x + "+" + y + ")");
    return x + y;
  }

  static int subtract(int x, int y) {
    logMessage("calculating (" + x + "-" + y + ")");
    return x - y;
  }

  private static void logMessage(String msg) {
    //Log it
  }

  static void main(String[] a) {
    System.out.println("Main. Executing from an interface");
  }
}
Code language: Java (java)

Hence, we can use the private static methods to encapsulate logic common to the Interface’s other static methods.

Can an Interface contain a public static void main Method?

The answer is yes; a Java Interface can contain our application’s main method (public static void main).

To demonstrate, let’s add the Main method to our Interface.

public interface Calculator {
  // Other methods
  static void main(String[] a) {
    System.out.println("Main. Executing from an interface");
  }
}Code language: Java (java)

Now, let’s compile and execute the class.

~ javac com/amitph/java/core/interfaces/staticint/Calculator.java
~ java com.amitph.java.core.interfaces.staticint.Calculator      
Main. Executing from an interface

The Interface can contain the Main method, and Java correctly invokes it when we run the Interface.

Summary

In this tutorial, we had a detailed introduction to the static methods in Java Interfaces. We learned about their behaviour and rules with the help of examples. We also created private static methods and wrote the main method inside an interface.

Refer to our Github Repository for the complete source code of the examples used in this tutorial.