Prior to java 8, a java interface could only have abstract methods and constants.
Starting java 8, an interface can also contain static methods and default methods.
This article will cover static interface methods in detail.

What are static interface methods
Static interface methods are just like static methods in a class, that is, these methods are preceded with static keyword.
Unlike primitive interface methods, static methods have a body, they do not end with a semi-colon.

Following are some important pointers that should be kept in mind regarding static interface methods.

  • static methods are preceded with static keyword.
  • static methods should have a body or implementation.
  • You can not invoke static interface methods using an object of a class that implements the interface.
  • static interface methods can be invoked using interface name and dot operator.
  • You can not define static interface methods whose signature matches with the methods defined in java.lang.Object class.
    Doing so would generate an error

    This static method cannot hide the instance method from Object

static interface method example
Below is an example of static method in an interface.

public interface StaticDemo {
   
   /**
    * Returns an error string if null, or string itself otherwise
    */
   static String normalize(String s) {
      System.out.println("Invoked static interface method"); 
      return s == null ? "Empty String" : s;
   }

   /** 
    * Print a string
    */
   void print(String s);
}

Above interface defines an abstract method and a static method. Example of a class implementing the interface is as below.
Note that it also overrides the abstract method of interface.

public class StaticImplementor implements StaticDemo {

   @Override
   void print(String s) {
      System.out.println("Invoked overridden method");
      System.out.println("Normalized string is: " + s);    
   }

   public static void main(String[] args) {
      String s = null;
      // invoke static interface method
      s = StaticDemo.normalize(s;
      // invoke class method
      StaticImplementor obj = new StaticImplementor();
      obj.print(s);
   }

}

This example first invokes the static interface method with a null string and then calls the overridden interface method producing following output.

Invoked static interface method
Invoked overridden method
Normalized string is: Empty String

Scope of static interface methods
If a class implements an interface with static method and it defines its own static method with the same name.
Now when it invokes the static method, which method should be invoked, the interface method or class’ own method.
Let’s check out.

public interface StaticDemo {
   
   /**
    * Returns an error string if null, or string itself otherwise
    */
   static void test() {
      System.out.println("Invoked interface method"); 
   }
}

This is an interface with a static method. Now a class that implements this interface and defines its own static method with the same name is given below.

public class StaticImplementor implements {

   // method with same name as interface
   static void test() {
      System.out.println("Invoked class method");
   }

   public static void main(String[] args) {
      // invoke method
      test()
   }

}

Above example prints

Invoked class method

which means that if a static method with same name is defined both in interface and class, then the method of class hides interface method.
Why static interface methods
This question might arise while reading as why static methods were added or what is the use of static methods in interface.
Following are two scenarios where static methods may come handy.

  1. Suppose there are utility or helper methods related to an interface functionality.
    Without static methods, you would need to create a separate class, put the methods there and then use it.
    But with static method support in interface, you can write all the methods in interface without writing an additional class.
  2. Static interface methods are used to create instance of the interface itself. Here, instance means an object of a class implementing the interface.
    Example is of method in java.util.stream.Stream interface or of method in java.util.List interface.

Static interface methods in java api
Following are some on the static methods in interface in java api.
of method in java.util.Map.
of method in java.util.Set.
of method in java.util.stream.Stream.
of method in java.util.List.
reverseOrder in java.util.Comparator.
naturalOrder in java.util.Comparator.
isEqual in java.util.function.Predicate.
and many more…
Hit the clap if the article was useful.

0

Share your thoughts !!

Close Menu

Never Miss an article !

Get the new post delivered straight into your inbox, enter your email and hit the button

You have successfully subscribed to the newsletter

There was an error while trying to send your request. Please try again.

codippa will use the information you provide on this form to be in touch with you and to provide updates and marketing.