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) {
...
}
}
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.
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 I were explaining this to someone I'd say we'll get to it later for now you need to know that the way to run your program is to use :
public static void main(String[] args) {
...
}
Assuming he/she knows what an array is, I'd say the args is an argument array and you can show some cool examples.
Then after you've gone a bit about Java/JVM and that stuff, you'd get to modifiers eventually to static and public as well.
Then you can spend some time talking about meaning of these IMHO.
You could mention other "cool" stuff such as varargs that you can use this in later versions of Java.
public static void main(String ...args) {
//...
}
To keep beginner attitude you can explain that all the command line is automatically split into an array of String (the String[]
).
For static
you have to explain, that it not a field like another : it is unique in the JVM even if you have thousand instances of the class
So main
is static, because it is the only way to find it (linked in its own class) in a jar
.
after you look at coding, and your job begin .
I just thought I'd chip in on this one. It's been answered perfectly well by others though.
The full main method declaration should be :
public static void main(final String[] args) throws Exception {
}
The args are declared final because technically they should not be altered. They are console parameters given by the user.
You should usually specify that main throws Exception so that stack traces can be echoed to console easily without needing to do e.printStackTrace() etc.
As for Array Syntax. I prefer it this way. I suppose that it's a little bit like the difference between french and english. In English it's "a black car", in french it's "a car black". Which is the important noun, car, or black?
I don't like this sort of thing :
String blah[] = {};
What's important here is that it's a String array, so it should be
String[] blah = {};
blah is just a name. I personally think it's a bit of a mistake in Java that arrays can sometimes be declared in that manner.
The normal usage of static is to access the function directly with out any object creation. Same as in java main we could not create any object for that class to invoke the main method. It will execute automatically. If we want to execute manually we can call by using main() inside the class and ClassName.main from outside the class.