You are here
Home > Java 8 >

Static Methods In Interface

Static Methods In Interface Before 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 in Interface as well. Now you might have one question โ€˜What is the need of defining Static methods in Interface?โ€™. Wait for a 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 things where we will run an interface (not a class) after declaring main() method in it. Needless to say, we will obviously look into the benefits of defining static methods in 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 in interface to use them as general utility methods.

How to define main() method inside an Interface ?

Since we can have static methods in interface starting from Java 8, we can define the 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 (since an abstract method does not contain an implementation). Default methods are defined with the default modifier, and static methods with the static keyword. Till Java 8, 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: Java 9 introduced private methods and private static methods in interfaces. Static methods in java 8 can not be private but from java 9 onward they can be.

Also read: Java Interfaces after Java 9

FAQ

Can a method be declared Default & Static together in an Interface?

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 an invalid method.

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

What is the benefit of using static methods in interfaces?

Static methods in interfaces offer a way to organize utility methods related to the interface. It helps in making the code more readable and maintainable. They also allow for code reuse without the need for a common base class.

Can interfaces have both static and default methods?

Yes, interfaces can have both static and default methods. Default methods provide a default implementation, while static methods are useful for utility methods.

Is it mandatory to provide an implementation for static methods in an implementing class?

No, itโ€™s not mandatory to provide an implementation for static methods in implementing classes. We can use the default implementation provided by the interface.

Thatโ€™s all about static methods in the Interface and its related concepts. For further details on this topic, kindly visit official documentation.

ย 

One thought on โ€œStatic Methods In Interfaceโ€

Leave a Reply


Top