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) {
...
}
}
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.
String args[] and String[] args are identical. In the sense that they do the same thing, Creating a string array called args. But to avoid confusion and enforce compatibility with all other Java codes you may encounter I'd recommend using the syntax (String[] args) when declaring arrays.
There is no difference between the two, aside from personal preference.
it means, the main method is not catching any exceptions, instead it handles the IOException by throwing it to the source which invoked the main method. You throw Exception if you want it to be handled by higher function or calling function.
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's main()
and the one true main()
.
String[] args
is a collection of String
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
public
: it is a access specifier that means it will be accessed by publically.static
: it is access modifier that means when the java program is load then it will create the space in memory automatically.void
: it is a return type i.e it does not return any value.main()
: it is a method or a function name.string args[]
: its a command line argument it is a collection of variables in the string format.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With