Java 8 - Static Method Inside interface

Static Method Inside interface:
We should call the static method using interface name.

Purpose:
* Just to define general utility methods. Which is helpful for multiple services.


interface Intefer{
public static void m1(){
System.out.println("interface static method")
}
}

class Test implements Interfer{
public static void main(String args[]){
Test t = new Test();
t.m1();  // Wrong statement
Test.m1();  // Wrong statement
Intefer.m1(); // Right statement
}
}


Overriding concept is not applicable for interface static methods.
Because interface static method is not available in the implemented class as per above example.

Some of the examples below,

interface Intefer{
public static void m1(){
System.out.println("interface static method")
}
}

Case 1: (Valid)
class Test implements Interfer{
public static void m1(){

}
}

Case 2: (Valid)
class Test implements Interfer{
public void m1(){

}
}

Case 3: (Valid)
class Test implements Interfer{
private static void m1(){

}
}

Here I have One doubt, We can able to declare static method, then why cant create main method and why cant we run the interface ?
You are right.. We can run the interface from Java 1.8

Comments

Post a Comment

Popular posts from this blog

How to set Java Object into JaxBElement ?

GitLab