The method signature of a Java mainmethod is:
public static void main(String[] args) { ... }
Is there a reason why this method must be static?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The method is static because otherwise there would be ambiguity: which constructor should be called? Especially if your class looks like this:
Should the JVM call
new JavaClass(int)? What should it pass forx?If not, should the JVM instantiate
JavaClasswithout running any constructor method? I think it shouldn’t, because that will special-case your entire class – sometimes you have an instance that hasn’t been initialized, and you have to check for it in every method that could be called.There are just too many edge cases and ambiguities for it to make sense for the JVM to have to instantiate a class before the entry point is called. That’s why
mainis static.I have no idea why
mainis always markedpublicthough.