You are here
Home > Java 8 >

Static Methods In Interface

Static Methods inside InterfaceBefore learning the Static Methods in Interface let’s go back to JDK 7 and older versions, and memorize the scope of a static method. We will come to a conclusion that Static Methods could be defined in a class, abstract class, final class but not in an interface. However, from JDK 8 onward, we can define Static Methods inside an Interface as well. Now you might have one question ‘What is the need of defining a Static method in an Interface?’. Wait for couple of minutes, you will get to know each & every concept behind it.

In this article we will have a closer look on the ‘Static Methods in Interface’. Furthermore, we will also talk about main() method as the main() method is itself a static method. We will go through one of the amazing thing where we will run an interface(not a class) after declaring main() method in it. Needless to say, we will obviously look into benefits of defining a static method inside an interface, it’s overriding behavior and so on.

What is a Static method ?

A static method is a method that is affiliated with the class in which it is defined rather than with any object. Every instance of the class shares its static methods.

What are the Static methods in Interfaces ?

Similar to Default Methods in Interfaces, Static Methods also have a method body (implementation). However, we can’t override them. Needless to say, static methods can’t be overridden.

How to declare a Static Method in Interface?

The Process of declaring a Static method in Interface is similar to defining a Static method in a class. In simple words, we have to use the static keyword while defining the method. For example, look at the code below.

interface A {
    public static void m1(){
      System.out.println("I am introduced in Java 1.8");
    }
}

Why we need a Static Method in Interface ?

If you want to provide some implementation in your interface and you don’t want this implementation to be overridden in the implementing class, then you can declare the method as static. Hence, we can secure an implementation by having it in static method as implementing classes can’t override it. Moreover, we define static methods inside classes to use them as general utility methods. Similarly, we can define static methods inside interfaces to use them as general utility methods.

How to define main() method inside an Interface ?

Since we can have static method starting from Java 8, we can define main method inside an interface without violating any rule of java. Also we can compile & run this interface successfully. For example, below code is valid.

interface Main {
    public static void main(String[] args){
       System.out.println("Define main() method inside interfaces now :)");
    }
}

Can we have main() Method inside an Abstract class & Enum ?

Apart from the interface as shown above, we can have main() method in an abstract class & enum as below. For example, below codes are also valid from the previous version of java. For abstract class it is valid from Java 1.4 while for enum from Java 1.5.

public abstract class B {
     public static void main(String[] args){
       System.out.println("I am inside abstract class");
     }
}

public enum C {
     ;
     public static void main(String[] args){
       System.out.println("I am inside enum");
     }
}

How to call static method of an Interface?

Here we are talking about static methods inside Interfaces only. They will be called with the name of Interfaces only. We can’t call them with the help of implementing classes or any other way. For example, observe the below code.

interface A {
    public static void m1(){
      System.out.println("call me with the Interface name");
    }
}

class Test implements A {
    public static void main(String[] args){
      A.m1(); //only way to call interface's static methods
    }
}

Can we Override Interface’s static methods ?

As aforementioned, Interface’s static methods can’t be overridden in implementing classes or in extending interfaces as they are not available to child classes by default. However, we can declare methods with the same signature in the implementing classes. They are valid, but we will not consider them as overridden methods. Hence, in this scenario method overriding concept is not valid. Also from the previous versions of Java, we know that static methods don’t participate in overriding.

interface A {
    public static void m1(){
      System.out.println("I am Interface's static method");
    }
}

class Test1 implements A {
    public static void m1(){
      System.out.println("I am not an overridden method");
    }
}

class Test2 implements A {
    public void m1(){
      System.out.println("I am not an overridden method");
    }
}

class Test3 implements A {
    private static void m1(){
      System.out.println("I am not an overridden method");
    }
}

For example, in the above code snippet all three implementing classes Test1, Test2 & Test3 contain a normal method m1(). We will not consider them as an overridden method but general method only.

How will you define an Interface after introduction of default & static methods?

An interface declaration can contain abstract methods, default methods, static methods and constant definitions. The only methods that have implementations are default and static methods. A class that implements an interface must implement all the abstract methods declared in the interface.

The interface body can contain abstract methods, default methods, and static methods. An abstract method within an interface is followed by a semicolon, but no braces (an abstract method does not contain an implementation). Default methods are defined with the default modifier, and static methods with the static keyword. All abstract, default, and static methods in an interface are implicitly public, so you can omit the public modifier. All constant declarations defined in an interface are implicitly public, static, and final. Once again, you can omit these modifiers as well.

Note on Default & Static Methods 

A method inside an Interface can’t be declared default & static together. Default methods can’t be static & static methods can’t be default. For example. the method m1() in below code snippet is invalid method.

interface A {
    default static void m1(){
      System.out.println("I am an invalid method"); // Invalid method
    }
}

That’s all about static methods in Interface and it’s related concepts. For further details on this topic, kindly visit official documentation.

 

close

One thought on “Static Methods In Interface

Leave a Reply

Top