Static Methods


Static methods

In this tutorial, we will discuss default methods in Java 8. Prior to Java 8, we could have only method declarations in the interfaces. But from Java 8, we can have default methods and static methods in the interfaces.

Static Methods in interface
  • From the 1.8 version onwards in addition to default methods, we can write static methods also inside the interface to define utility functions.
  • Interface static methods by default are not available to the implementation classes hence by using implementation class reference we can’t call interface static methods. We should call interface static methods by using the interface name.
package com.ashok.java8.staticmethods;

/**
 * 
 * @author ashok.mariyala
 *
 */
public interface MyInterface {
   public static int multiply(int a, int b) {
      return a * b;
   }
}
package com.ashok.java8.staticmethods;

/**
 * 
 * @author ashok.mariyala
 *
 */
public class Test implements MyInterface {
   public static void main(String[] args) {
      Test t = new Test();
      System.out.println(t.multiply(10, 20)); // C.E
      System.out.println(Test.multiply(10,20)); // C.E
      System.out.println(MyInterface.multiply(10,20));
   }
}
  • As interface static methods by default are not available to the implementation class, the overriding concept is not applicable.
  • Based on our requirement we can define exactly the same method in the implementation class, it’s valid but not overriding.

E.g

package com.ashok.java8.staticmethods;

/**
 * 
 * @author ashok.mariyala
 *
 */
public interface MyInterface {
   public static int multiply(int a, int b) {
      return a * b;
   }
}
package com.ashok.java8.staticmethods;
/**
 * 
 * @author ashok.mariyala
 *
 */
public class Test implements MyInterface {
   public static int multiply(int a, int b) {
      return a * b;
   }
}

It is valid but not overriding.

E.g 2

package com.ashok.java8.staticmethods;

/**
 * 
 * @author ashok.mariyala
 *
 */
public interface MyInterface {
   public static int multiply(int a, int b) {
      return a * b;
   }
}
package com.ashok.java8.staticmethods;
/**
 * 
 * @author ashok.mariyala
 *
 */
public class Test implements MyInterface {
   public int multiply(int a, int b) {
      return a * b;
   }
}

It is valid but not overriding.

E.g 3

package com.ashok.java8.staticmethods;

/**
 * 
 * @author ashok.mariyala
 *
 */
public class MyClass {
   private int multiply(int a, int b) {
      return a * b;
   }
}
package com.ashok.java8.staticmethods;
/**
 * 
 * @author ashok.mariyala
 *
 */
public class Test extends MyClass {
   public int multiply(int a, int b) {
      return a * b;
   }
}

It is valid but not overriding.

From the 1.8 version onwards we can write the main() method inside the interface and hence we can run the interface directly from the command prompt.

package com.ashok.java8.staticmethods;

/**
 * 
 * @author ashok.mariyala
 *
 */
public interface MyInterface {
   public static void main(String args[]) {
      System.out.println("Hi this is ashok..!!");
   }
}
Static Methods
Scroll to top