Java9 - How to declare Private Static methods in the interface

In this blog post, Learn about Java 9’s interface private methods and static methods with examples. Java has seen many versions, and this post focuses on the evolution of interfaces from public modifiers to private modifiers and from abstract methods only to non-abstract methods.

Java 7 Interface Declaration Version Examples

Before Java 7, interfaces contained only constants and abstract methods.

Examples include

-constants i.e. public static final variables.

  • Public abstract methods with no implementation( public abstract method();0)
public interface MyInterface {
    public String message = "Hello";
    public void getMessage();
}

In the above interface example, we have defined one constant and one abstract method. These methods lack implementation, only having declarations, thus termed abstract. To include non-abstract methods, one could opt for an abstract class with abstract methods.

Java 8 Interface Default and Static Methods with an Example

Java 8 introduced several features to interfaces.

It allowed the declaration of public default methods and public static methods within interfaces.

Thus, interfaces could contain the following things.

  • public static final variables
  • public abstract methods
  • public Default methods
  • public static methods

Here is an example

public interface MyInterface {
    public String CONSTANT = "constant";
    public void abstractMethod();
    // default method example
    public default void defaultMethod() {
        System.out.println("defaultMethod");
    }
    // static method example
    public static void staticMethod() {
        System.out.println("staticMethod");
    }
}
public class TestInterface implements MyInterface{
    public static void main(String[] args) {
        TestInterface t=new TestInterface();
        t.abstractMethod();
        t.defaultMethod();
        MyInterface.staticMethod();
    }
    @Override
    public void abstractMethod() {
        System.out.println("abstractMethod");
    }
}

Output:

abstractMethod
defaultMethod
staticMethod

Default methods can be overridden and called using an instance of the implementation class, while static methods cannot be overridden and are called using the interface name.

Java 9 Interface Private and Static Methods Example

Java 9 introduced the ability to add private methods and private static methods to interfaces.

For instance, you may have written common code within default methods in the interface.

The implementation class often contains redundant code, which is not considered a good design.

To address this, we can move redundant code to private methods to promote reusability.

However, it’s important to note that private methods enable code reuse within an interface.

Implementation classes will have access to these private methods and the below features.

  • public static final variables
  • public abstract methods
  • public Default methods
  • public static methods
  • private methods
  • private static methods

Here is a java9 private static interface method example

public interface MyInterface {
    public String CONSTANT = "constant";
    public void abstractMethod();
    // default method example
    public default void defaultMethod() {
        privateMethod();
        privateStaticMethod();
        System.out.println("defaultMethod");
    }
    // static method example
    public static void staticMethod() {
        System.out.println("staticMethod");
    }
    // private method example
    private void privateMethod() {
        System.out.println("privateMethod");
    }
    // private static method example
    private static void privateStaticMethod() {
        System.out.println("privateStaticMethod");
    }
}
public class TestInterface implements MyInterface{

    public static void main(String[] args) {
        TestInterface t=new TestInterface();
        t.abstractMethod();
        t.defaultMethod();
        MyInterface.staticMethod();
    }
    @Override
    public void abstractMethod() {
        System.out.println("abstractMethod");

    }

}

The output of the above code execution is

abstractMethod
privateMethod
privateStaticMethod
defaultMethod
staticMethod

The error Modifier 'private' not allowed here will be thrown in versions 8 or earlier.

Private and Static Methods Interface Features

  • These methods are denoted as private with the private keyword.
  • Private methods include bodies enclosed in curly braces.
  • The abstract keyword does not apply to these methods, as both functionalities are reversed.
  • Private methods are accessible only within the interface where they are declared.
  • Private static methods can access other static and non-static methods, i.e., default methods
  • Private methods cannot be used in private static methods.

Ensure proper usage of these methods in interfaces to avoid a Exception in thread “main” java.lang.StackOverflowError’.

Conclusion

In this tutorial, we’ve learned about private methods and static methods in Java 7, Java 8, and Java 9 versions, along with examples.