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

Why main method in java is static?

I go through many explaination..everywhere something like "Java program's main method has to be declared static because keyword static allows main to be called without creating an object of the class in which the main method is defined" But as we know non-static method can call both static and non-static....where static can only call static. Isn't it would be better that main method would be non-static....we don't even need to create object too!??? Why it's static? And who call main method??? Request...please answer my doubt it's killing me inside

7th Mar 2020, 11:44 PM
I Am a Baked Potato
I Am a Baked Potato - avatar
3 Answers
8th Mar 2020, 12:13 AM
Kevin ★
+ 2
When the runtime starts, there is no object of the class is created. If it is a static method, there would be obscurity. When the JVM searches for an entry point of the class, instead just implicitly calling the class itself, the JVM will call the constructor of the class, which should not happen. I'm not particularly sure of the reason but I think it's for convenience.
8th Mar 2020, 12:05 AM
Nootnoot
Nootnoot - avatar
+ 1
What you have written in quotes is correct. Yes you cant call non static methods from a static method but you can create objects in a static method which can call non static methods. That is happening in the main method too. But main method has to be static cause who would create the object which could call the non static main method. Thats like then egg and chicken question (which has been there first). The main method is the entery point into the programm. The String[] args are arguments which can be passed into the programm when executing the programm.
8th Mar 2020, 12:12 AM
Jnn
Jnn - avatar