Teen Programmers Unite  
 

 

Return to forum top

A Query on Java

Posted by vikram_1982 [send private reply] at April 02, 2002, 01:28:03 AM

Why do I need to pass "String args[]" as a parameter for the main class? What is its functionaliy?? I dont see any purpose. Also, why do I declare the main class as Static??

Posted by gian [send private reply] at April 02, 2002, 03:57:20 AM

I can answer the first one for you... args is your list of arguments passed to the program. Just like argv in C/C++ or $1 in shell scripting!

eg. if I was to invoke the program called "bob" with the argument "jim", then:

(I will write in psuedo-code, as my java will get laughed out of town)

main( String args[] )
{
     print "My best friend's name is " args[0] "!"
}


When invoked like "friendprogram jim", then it should print
 My best friend's name is jim! 


Args is an array because it seperates the command line arguments into array elements, like:

main( String args[] )
{
     print "My best friend's name is " args[0] "!\n"
     print "My dog's name is " args[1] "!"
}


Would, when invoked like "friendprogram jim bob" would output:

 My best friend's name is jim!
   My dog's name is bob!


See?
Posted by vikram_1982 [send private reply] at April 02, 2002, 07:15:04 AM

Gian,
Thanks 4 the explanation. But, while using Command Line arguments( Say in C/C++) the usage of args and argv are resonable. Also, in C/C++, if we arent using command line arguments, we needn't use args and argv, right. But why does Java insist that we use it at all times ( the command line arguments).

Vikram.

Posted by taubz [send private reply] at April 02, 2002, 02:57:52 PM

It's just a peculiarity of the language. The runtime simply expects to find a function main with that exact signature.

The reason the method is declared as static is that the runtime needs to be able to run that function, but it doesn't want to make a new instance of the class that it's declared in. EX:

If it's not declared as static, the VM would have to do:
new MyMainClass().main(arguments);

But if it's static, it can more efficiently do:
MyMainClass.main(arguments);

- taubz

Posted by Psion [send private reply] at April 02, 2002, 03:03:46 PM

In C, if you leave out parameters to main, they pretty much get silently added. A program run at the command line is _always_ passed its own name as the first command line parameter, for instance. In Java, things are done more modularly, and command line programs are no longer at the center of the language. There has to be a single method to call to run a program, so that method needs to have all of the parameters needed to support what coders might want to do.

Posted by vikram_1982 [send private reply] at April 03, 2002, 12:39:38 AM

Thanks All,
That really helped.

You must be logged in to post messages and see which you have already read.

Log on
Username:
Password:
Save for later automatic logon

Register as a new user
 
Copyright TPU 2002. See the Credits and About TPU for more information.