Interface Static Method in Java 8 Example

With the release of Java 8, we can also define static method within an interface. A static method within an interface is explicitly defined with a static keyword and method body.

It works nearly the same as static methods defined in classes. A static method defined in Java 8 interface is not inherited in any classes that implement the interface.

Rules of define Static method in Java 8 Interface


There are the following static interface method rules that you need to be familiar with:

1. Like all methods defined within an interface, a static method is considered to be public and will not compile if declared as private or protected.

2. To call the static method, a reference to the name of the interface must be used.

3. Abstract keyword cannot be used with static methods within an interface.

4. We cannot use the default keyword for static method because a default method is an instance in nature.

5. We cannot override the static method in the implementation class because the static methods are not inherited by the subclass and must be accessed with a reference to the interface name.

Static Method Interface Example Programs


Let’s take an example program where we will declare a static method within an interface. Look at the source code to understand better.

Program source code 1:

public interface Perimeter 
{ 
  void msg(); 
  static int peri(int l, int b)
  { 
     return 2*(l + b); 
  } 
} 
public class Rectangle implements Perimeter 
{ 
  public void msg()
  { 
     System.out.println("Perimeter of rectangle"); 
  } 
public static void main(String[] args) 
{ 
   Perimeter p = new Rectangle(); 
     p.msg(); 
   int perimeter = Perimeter.peri(20,30); 
   System.out.println(perimeter); 
  } 
}
Output: 
        Perimeter of rectangle 100

Let’s take another example program based on this concept.


Program source code 2:

public interface A {	
 public default int m1() // Default method.
 {
   return 10;	 
 }
 int m2(); // Normal abstract method.
 static int m3() // Static method declaration within an interface.
 {
   return 30;
 }
}
public class B implements A{
 public int m2() // Overriding abstract method.
 {
  return 20; 
 }
}
public class AB {
public static void main(String[] args) 
{
  B b = new B();
  System.out.println(b.m1()); // Calling default method.
  System.out.println(b.m2()); // Calling abstract method of implementation class.
  System.out.println(A.m3()); // Calling static method using name of interface.
 }
}

Output:
       10
       20
       30

Explanation:

1. As you observe in this example, the method m3() works just like a static method as defined in a class. In other words, it can be accessed without an instance of the class using the A.m3() syntax.

2. Note that the compiler will automatically insert the access modifier public because all methods in an interface are considered to be public.

3. If we try to call static method without an explicit reference to the name of the interface, the code will not compile.

Program source code 3:

public interface A {	
 public default void m1() // Default method.
 {
   System.out.println("Default method defined within an interface"); 
 }
 void m2(); // Normal abstract method.
 static void m3() // Static method declaration within an interface.
 {
   System.out.println("Static method defined within an interface");
 }
}
public class B implements A{
 public void m2() // Overriding abstract method.
 {
   System.out.println("Implementation of abstract method");	 
 }
 static void m3(){
 System.out.println("Static method defined within implementation class");	 
 }
}
public class AB {
public static void main(String[] args) 
{
 B b = new B();
 b.m1();
 b.m2();
 A.m3();
 B.m3();
  }
}
Output:
      Default method defined within an interface
      Implementation of abstract method
      Static method defined within an interface
      Static method defined within implementation class

Explanation:

As you can observe in this example, static method cannot be inherited in the derived class and can be accessed with a reference to the name of interface.

Program source code 4:

public interface A {	
 static void m1()
 {
   System.out.println("Static method in A");
 }
}
public interface B {
 static void m1(){
 System.out.println("Static method in B ");	 
 }
}
public class AB implements A, B{
public static void main(String[] args) 
{
 A.m1(); // Calling static method of interface A.
 B.m1(); // Calling static method of interface B.
  }
}
Output:
       Static method in A
       Static method in B

Hope that this tutorial has covered all the important rules of defining static method in Java 8 interface with example program. I hope that you will have understood the basics of Java 8 static interface method.
Thanks for reading!!!
Next ⇒ Polymorphism in Java⇐ PrevNext ⇒