Explanation of 'String args[]' and static in 'public static void main(String[] args)' [duplicate]
I would break up
public static void main(String args[])
in parts:
public
It means that you can call this method from outside of the class you are currently in. This is necessary because this method is being called by the Java runtime system which is not located in your current class.
static
When the JVM makes call to the main method there is no object existing for the class being called therefore it has to have static method to allow invocation from class.
void
Java is platform independent language and if it will return some value then the value may mean different things to different platforms. Also there are other ways to exit the program on a multithreaded system. Detailed explaination.
main
It's just the name of method. This name is fixed and as it's called by the JVM as entry point for an application.
String args[]
These are the arguments of type String that your Java application accepts when you run it.
I would point a beginner to the Wiki article on the Main function, then supplement it with this.
Java only starts running a program with the specific
public static void main(String[] args)
signature, and one can think of a signature like their own name - it's how Java can tell the difference between someone else'smain()
and the one truemain()
.String[] args
is a collection ofString
s, separated by a space, which can be typed into the program on the terminal. More times than not, the beginner isn't going to use this variable, but it's always there just in case.
public static void main(string [] args)
public
-its the access specifier means from every where we can access it;
static
-access modifier means we can call this method directly using a class name without creating an object of it;
void
- its the return type;
main
- method name
string [] args
- it accepts only string type of argument... and stores it in a string array