Ask your own question, for FREE!
Computer Science 9 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!
Latest Questions
Arriyanalol: i need help
1 day ago 7 Replies 1 Medal
Arriyanalol: CONGRATS to me
1 day ago 8 Replies 2 Medals
Legomaster346: What colleges are good that are near Arab Alabama?
1 week ago 5 Replies 0 Medals
Arriyanalol: i need help
1 week ago 6 Replies 2 Medals
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!