The method signature of a Java main() method is:
public static void main(String[] args){
...
}
Is the
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 of new Program()
).static void main()
main()
.void main()
new ClassName()
.main()
.new ClassName()
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 the main()
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 a main()
method which itself constructs an instance of a class and calls an instance method, there is no real advantage to specifying main()
as an instance method.