Explanation of 'String args[]' and static in 'public static void main(String[] args)'

Sam picture Sam · Aug 14, 2012 · Viewed 435k times · Source

How can you explain very well, to a beginner, the meaning of String args[] and the use of static in the following excerpt?

class FirstApp {
    public static void main(String[] args) {
        ...
    }
}

Answer

Confuse picture Confuse · Feb 11, 2015

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.