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?
What is the meaning of
public static void main(String args[])
?public
is an access specifier meaning anyone can access/invoke it such as JVM(Java Virtual Machine.static
allowsmain()
to be called before an object of the class has been created. This is neccesary becausemain()
is called by the JVM before any objects are made. Since it is static it can be directly invoked via the class.Similarly, we use static sometime for user defined methods so that we need not to make objects.
void
indicates that themain()
method being declared does not return a value.String[] args
specifies the only parameter in themain()
method.args
- a parameter which contains an array of objects of class typeString
.static - When the JVM makes a call to the main method there is no object that exists for the class being called therefore it has to have static method to allow invocation from class.
It is just a convention. The JVM could certainly deal with non-static main methods if that would have been the convention. After all, you can define a static initializer on your class, and instantiate a zillion objects before ever getting to your main() method.
Let me explain these things in a much simpler way:
All Java applications, except applets, start their execution from
main()
.The keyword
public
is an access modifier which allows the member to be called from outside the class.static
is used because it allowsmain()
to be called without having to instantiate a particular instance of that class.void
indicates thatmain()
does not return any value.When you execute the Java Virtual Machine (JVM) with the
java
command,When you execute your application, you specify its class name as an argument to the java command, as above
the JVM attempts to invoke the main method of the class you specify
let's back to the command
ClassName
is acommand-line argument
to the JVM that tells it which class to execute. Following the ClassName, you can also specify alist of Strings
(separated by spaces) as command-line arguments that the JVM will pass to your application. -Such arguments might be used to specify options (e.g., a filename) to run the application- this is why there is a parameter calledString[] args
in the mainReferences:Java™ How To Program (Early Objects), Tenth Edition
Let's simply pretend, that
static
would not be required as the application entry point.An application class would then look like this:
The distinction between constructor code and
main
method is necessary because in OO speak a constructor shall only make sure, that an instance is initialized properly. After initialization, the instance can be used for the intended "service". Putting the complete application code into the constructor would spoil that.So this approach would force three different contracts upon the application:
main
method1. Ok, this is not surprising.abstract
. Otherwise, the JVM could not instantiate it.The
static
approach on the other hand only requires one contract:main
method1.Here neither
abstract
nor multiple constructors matters.Since Java was designed to be a simple language for the user it is not surprising that also the application entry point has been designed in a simple way using one contract and not in a complex way using three independent and brittle contracts.
Please note: This argument is not about simplicity inside the JVM or inside the JRE. This argument is about simplicity for the user.
1Here the complete signature counts as only one contract.