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
3 Respuestas
+ 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.
+ 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.