The method signature of a Java main() method is:
public static void main(String[] args){
...
}
Is there a reason for this method to be static?
The method signature of a Java main() method is:
public static void main(String[] args){
...
}
Is there a reason for this method to be static?
I think the keyword 'static' makes the main method a class method, and class methods have only one copy of it and can be shared by all, and also, it does not require an object for reference. So when the driver class is compiled the main method can be invoked. (I'm just in alphabet level of java, sorry if I'm wrong)
The true entry point to any application is a static method. If the Java language supported an instance method as the "entry point", then the runtime would need implement it internally as a static method which constructed an instance of the object followed by calling the instance method.
With that out of the way, I'll examine the rationale for choosing a specific one of the following three options:
static void main()
as we see it today.void main()
called on a freshly constructed object.Program
, then the execution would effectively consist ofnew Program()
).Breakdown:
static void main()
main()
.void main()
new ClassName()
.main()
.new ClassName()
Rationale:
I'll go in reverse order for this one.
Keep in mind that one of the design goals of Java was to emphasize (require when possible) good object-oriented programming practices. In this context, the constructor of an object initializes the object, but should not be responsible for the object's behavior. Therefore, a specification that gave an entry point of
new ClassName()
would confuse the situation for new Java developers by forcing an exception to the design of an "ideal" constructor on every application.By making
main()
an instance method, the above problem is certainly solved. However, it creates complexity by requiring the specification to list the signature of the entry class's constructor as well as the signature of themain()
method.In summary, specifying a
static void main()
creates a specification with the least complexity while adhering to the principle of placing behavior into methods. Considering how straightforward it is to implement amain()
method which itself constructs an instance of a class and calls an instance method, there is no real advantage to specifyingmain()
as an instance method.The
public
keyword is an access modifier, which allows the programmer to control the visibility of class members. When a class member is preceded bypublic
, then that member may be accessed by code outside the class in which it is declared.The opposite of
public
isprivate
, which prevents a member from being used by code defined outside of its class.In this case,
main()
must be declared aspublic
, since it must be called by code outside of its class when the program is started.The keyword
static
allowsmain()
to be called without having to instantiate a particular instance of the class. This is necessary sincemain()
is called by the Java interpreter before any objects are made.The keyword
void
simply tells the compiler thatmain()
does not return a value.It is just a convention as we can see here:
http://docs.oracle.com/javase/1.4.2/docs/tooldocs/windows/java.html#description
Recently, similar question has been posted at Programmers.SE
TL;DR part of the accepted answer is,
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
JavaClass
without 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
main
is static.I have no idea why
main
is always markedpublic
though.