Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

In Java main()

Status
Not open for further replies.

gobinda77

Programmer
Oct 27, 2002
3
IN
Why the main in any class
public static void main(java.lang.String[] args)

needs String[] args as parameters???
 
Not every JAVA Class, but every JAVA program requires a Main method. The Main method can be seen as the startup method, that is executed when you start the program. The String args[] parameters contains the the parameters that you pass to the program on the JAVA commandline.

Example: java yourprogram parm1 parm2

passes parm1 and parm2 as String args[0] and args[1] to your Main method in program yourprogram.

Best regards,

Tom.
 
I'm not sure if the compiler will allow you to compile without it anymore (been a while). But look at it this way, if you did write your code with the array in your main statement and then attempted to run your program, but accidentally (or someone else intentionally) included parameters after the java yourprogram, what would happen? since the only available function is main() but it is not accepting I would assume (and will shortly try this out myself) that you are gping to get at the very least a method not found error.

-Tarwn --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
For my next trick I will pull a hat out of a rabbit (if you think thats bad you should see how the pigeon feels...) :p
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top