Ask your own question, for FREE!
Computer Science 10 Online
OpenStudy (myname):

why does the main method has to be static in java? Cant it be non-static? What is the main reason?

OpenStudy (anonymous):

Consider if the main method were non-static, and the class had a parameterised constructor: public class MyClass { public MyClass(string y) { /* ... */ } public void main(String[] args) { /* ... */ } } If main() is non-static, then it must be called on an instance of MyClass. But, how can the runtime create an instance of MyClass? It has no idea what the parameter represents, so it can't give it a value, so it cannot create an instance of the class, so it can't access the main method! The runtime side-steps the whole issue by requiring the method be marked static, rather than add specific, complicated rules about the main() class' constructors. I can't think of an instance where this requirement would cause any problems. After all, you are free to create your own instance of MyClass in a static main().

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!