Can Interface have main method ?

First of all try to look at the following code and execute it on your own.

public interface testinterface1
{
String message = "welcome to this group";
static int add ( int a, int b)
{
return a+b;
}
public static void main(String j[])
{
System.out.println(message);
int x=5;
int y=15;
System.out.println(" sum of numbers " + x + " and " + y + " is " + add(x,y));
}
} 

Very first though we may have that this code will not even compile at all and it is true but this will compile in java8. Yes this is true, we can have main method in interface in java8. so try it in java8 and this code will compile for sure. 

No comments:

Post a Comment