55

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) {
        ...
    }
}
Jamal
  • 763
  • 7
  • 22
  • 32
Sam
  • 3,067
  • 19
  • 53
  • 55
  • Only one `main()` can run, and arrays of `String` are a bit advanced for the beginner. I've only ever used them in slightly more advanced programs, which required iterating over an array/collection. – Makoto Aug 14 '12 at 13:02
  • 8
    How much do you want to explain it, and how much of a beginner are they? When I first started Java, my professor just said "that's the way it is" until after we started creating our own functions. After that, he explained what the String args[] is for. – Brian J Aug 14 '12 at 13:04
  • 5
    Should probably be `String[] args`, and not `String args[]` or your beginner will be forever confused about array syntax. – Joel Westberg Aug 14 '12 at 13:16
  • 2
    @Eng.Fouad That question does not address `static`. Also, it's not about the use of `args` as input arguments. – S.L. Barth is on codidact.com Aug 14 '12 at 13:17
  • 2
    @JoelWestberg: Either are acceptable, although it's convention to put it around the type, and not the variable. Then again, there's varargs which works as a signature to main... – Makoto Aug 14 '12 at 13:21
  • 2
    @Makoto, I know. But from experience having taught introductory courses in Java, the principle of least surprises should put `[]` as part of the type and not the name of the variable. – Joel Westberg Aug 14 '12 at 13:49
  • 1
    I was told that this would be explained later when being taught, but I missed the explanation later! This did not help me as a beginner. I would venture into opinion territory and say that in my experience, being explained things as we go (even briefly) is preferable. Especially that the main() function is so vital. – Möoz Jan 07 '14 at 21:42
  • @JoelWestberg I program in C++ so I believe it is more intuitive for some of us to use String args[] – Keshav Garg Jul 27 '20 at 12:05

8 Answers8

92

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.

Community
  • 1
  • 1
Confuse
  • 5,646
  • 7
  • 36
  • 58
34

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 Strings, 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.

Simone Carletti
  • 173,507
  • 49
  • 363
  • 364
Makoto
  • 104,088
  • 27
  • 192
  • 230
17
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

whackamadoodle3000
  • 6,684
  • 4
  • 27
  • 44
user3830480
  • 179
  • 1
  • 2
8
  • 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.
Andrew
  • 4,953
  • 15
  • 40
  • 58
3

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) {
        //...
    }
ant
  • 22,634
  • 36
  • 132
  • 182
1

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 .

KR Akhil
  • 877
  • 3
  • 15
  • 32
cl-r
  • 1,264
  • 1
  • 12
  • 26
  • Exact, ... but it is to a beginner. Do you use multiple loader of main class? I do no guess the Pattern or the utility to do it. – cl-r Aug 14 '12 at 13:19
1

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.

Richard
  • 1,070
  • 9
  • 22
  • 2
    Well.. The declaration of the form: `String blah[] = {};` was included in the later versions of Java to help the programmers coming from C and C++ backgrounds. – Vikas Dec 25 '16 at 21:19
  • Interesting, but it is confusing to people that are noobs to coding, such as myself: (String[] args) vs. (String args[]). If it should be String[] then conform to that. – SamAndrew81 Aug 15 '18 at 03:49
  • 1
    It's one of only a small number of 'style' issues Java suffers from. The classic is where you place the curly braces, it's a source of endless arguments in offices. I like 'Egyptian Notation', but some of my colleagues don't, and I hate them deeply. Stick with it, Java is out of fashion with the younger generation, but they think JSON is a replacement for XML, so I'd take what they say with a pinch of salt. – Richard Aug 15 '18 at 10:35
0

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.

Bathakarai
  • 1,517
  • 6
  • 23
  • 39