GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes String argument in main method() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String argument in main method()" Watch "String argument in main method()" New topic
Author

String argument in main method()

Harjeet Dadwal
Greenhorn

Joined: Sep 09, 2001
Posts: 17
In main mehod there is a one paramenter is given as string
*** public static void main(String[] args) ***
string args is given to pass comandline argument but if we don't pass any argument then it runs without any error but as a function or method rule if we define a function or method having an argument then must be passed while calling that method but if we don't pass any commandline argument then how it works....?

------------------
Harjeet Singh Dadwal


Harjeet Dadhwal
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
Since you are not passing in any parameters from the comand line, the args array will have a length of zero. Note that you are not specifically calling this method. The method gets executed by the JVM automatically.

Bosun


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Harjeet Dadwal
Greenhorn

Joined: Sep 09, 2001
Posts: 17
if this is activated by JVM then it is OK otherwise according rule it is illegal to call a function without specifying a parameter which has already been defined while function is defined, Am I right ?
However Very thanks for your reply,
thanks again
------------------
Harjeet Singh Dadwal
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

the JVM *does* supply a parameter.

If you invoke your program with parameters, the JVM constructs a String array of the parameters, and sends it to main.

If you invoke the program with no parameters, the JVM constructs an empty String array, and sends it to main.
No one breaks any rules in this example. But you are correct. If the method defines that you must pass two integers, or three floats, then any call to that method must pass those parameters, otherwise it is a compiler error.
 
GeeCON Prague 2014
 
subject: String argument in main method()