why main method is static? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

why main method is static?

Classes are introduced into the Java environment when they are referenced by name in a class that is already running. There is a bit of magic that goes on to get the first class running (which is why you have to declare the main() method as static, taking a string array as an argument), but once that class is running, future attempts at loading classes are done by the class loader. if the reason is correct then can u explain?

18th Jul 2017, 7:45 AM
Somnath Ghosh
Somnath Ghosh - avatar
1 Answer
+ 19
The main method must be public so it can be found by the JVM when the class is loaded. Similarly, it must be static so that it can be called after loading the class, without having to create an instance of it.
18th Jul 2017, 7:54 AM
Nithiwat
Nithiwat - avatar