+2

Using static keyword with main()

Why do we use static keyword with the main() function, please, describe in brief?

9/15/2019 4:40:30 AM

preetpal singh

6 Answers

New Answer

+6

What I'm saying is that the static keyword eliminates the ambiguity the constructor for the main method would cause. After all, how would the JVM know which constructor to call, if your main class had overloaded constructors? What parameters would it pass? There's just too many questions to ask and answer here

+9

The method is static because otherwise you would need to write a constructor just to get the main method to run

+3

Somewhere I have read this explanation: A static method belongs to the class and not only to the instance. If the main method would be non static you would need to create an instance. But to create an instance you need the main method which executes your code.

+1

But we describe the statements in the main method, why would someone want to write a constructor for the main?

+1

Yes, you are right there are too many doubts about the main() method itself. Anyway, thanks for your assistance.

+1

Ok, if we are gonna call the main function using classname.main()(as it is static) in other class, what will happen then?