Interface main method | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Interface main method

Every java class starts from main() method. And if we mention main() method in interface and run it directly then main() executes, how? Please someone explain.

9th Nov 2020, 2:29 PM
prakash
prakash - avatar
2 Answers
+ 4
First thing you must understand is by default every class or interface you create are saved with .java extension. So the JVM is only looking for a main() which is public, static and void. It doesn't matter if it is inside a public class or interface. So until Java version 7 this was not possible because you cannot declare static methods inside an interface. But Java 8 introduced static and default methods in interface which made this possible. This code will show the version used and a statement inside the interface. https://code.sololearn.com/cj3e8s6NIm6W/?ref=app
9th Nov 2020, 3:32 PM
Avinesh
Avinesh - avatar
+ 1
yup got it now. since jdk 8, its possible, checked on older version too. interface treat main() method as regular static method but JVM search for main(). class and interface are equal for JVM. Thank you.
9th Nov 2020, 5:38 PM
prakash
prakash - avatar