• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why do we give "String [] args" as an argument to main??  RSS feed

 
Prince Sewani
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do we give "String [] args"

as an argument to main??

i know we can read if the user gives input through command line while running the program,
but when i compile without giving that it compiles safe,but when i run it doesn't ??
the terminal says "Exception in thread "main" java.lang.NoSuchMethodError: main"

can someone throw some light at it??


==================================
Please consider me an absolute beginner to java.
==================================

 
Harnoor Singh
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is predefined method in Java from where the program execution starts which is "void main(String [] args)", in your case there is no such method because you have changed the method signature,....
 
Prince Sewani
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Harnoor

from your answer do you mean to say that that's the pre-defined syntax?? right??

if yes then what does that actually specify like we have reasons for other pieces too as 'static','public',etc.. :p
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you type "java MyClass", the JVM goes and looks for a method in MyClass with the signiature "public static void main (String [] args)" (although the specific name of the String array can be anything you want).

If that method is not there, the JVM can't start up your program.

However, it is perfectly valid to have a class that does not have that main method. You will write many classes that you use in your program, but only one will house the starting point. I may create a "HelperClass" where I create instances of it in the main method, but I would never start my program by calling HelperClass's main() method.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read all about it here:
http://java.sun.com/docs/books/jls/third_edition/html/execution.html
 
Prince Sewani
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Fred,

And Darry,thanks for the cool link. thanks a lot guys.
 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And wecome to JavaRanch
 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prince Sewani wrote: . . . do you mean to say that that's the pre-defined syntax?? right?? . . .
No, it is an inaccurate use of "pre-defined." He means required, because that is what the JVM is looking for, very specifically.
 
Prince Sewani
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Thanks Sherif..i mean Campbell, just wish to grow myself interacting with the people around here,and may be become a part of the active solution provider team at this forum some time in future.

Campbell Ritchie wrote:
Prince Sewani wrote: . . . do you mean to say that that's the pre-defined syntax?? right?? . . .
No, it is an inaccurate use of "pre-defined." He means required, because that is what the JVM is looking for, very specifically.
 
Matthew Mellott
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To spice things up, you can change args to cheese:

public static void main(String cheese[])

This does not change the method signature, and, as others have said above, the main method with the above signature is specifically looked for as the entry point of your application.
 
Prince Sewani
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Mellott wrote:To spice things up, you can change args to cheese:

public static void main(String cheese[])

This does not change the method signature, and, as others have said above, the main method with the above signature is specifically looked for as the entry point of your application.



Looks like Mathew is waiting for breakfast...

Hey man i got the reason behind that one,thanks to all for replying. Thanks to you to Mathew.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!