Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the following code working with an interface but without any class defined? [duplicate]

Tags:

interface Main 
{
public static void main(String[] args) 
{
    System.out.println("Inside main");
    int a = 4 , b = 6 ;
    System.out.println(a+b);
}
}

In the above code, there is no class defined but the program is still getting executed. But as far as I know, there cannot be any static method inside an Interface. And, every program should contain at least one main function.

like image 542
akisonlyforu Avatar asked Feb 19 '18 17:02

akisonlyforu


1 Answers

Because, you are using Java version 8.

From Java 8 on, you are allowed to have static methods inside an interface.

And main() gets run from interfaces as well (even from enums), as long as you keep the correct signature.

like image 198
elyor Avatar answered Sep 23 '22 13:09

elyor