can anyboyd pls explain this,i got a class with main() without string args,it compiles but doesnt run.,i know why main is declared as static and public,but what is exactly string args[] doing in main() method? public class mainwithoutargs { public static void main() { System.out.println("im in main w/o string args[]"); } }
please use the [code][/code] tags when showing code. visit <a href="http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=ubb_code_page" target="_blank" rel="nofollow">http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=ubb_code_page</a> ,for more details
the string args in main method is the string array which holds any arguments invoked on the command line. Java requires you to have a main with signature(string[]) so as to run that application. For example try this program
and invoke it java mainwithargs one two see the results.
Originally posted by srinivas bolloju: can anyboyd pls explain this,i got a class with main() without string args,it compiles but doesnt run.,i know why main is declared as static and public,but what is exactly string args[] doing in main() method? public class mainwithoutargs { public static void main() { System.out.println("im in main w/o string args[]"); } }
[This message has been edited by Anshul Manisha (edited July 06, 2001).]
hi anshul,in the class which u posted, u are expecting i.e the class is expecting some arguments to be passed, so args[0] prints one, in my class ,im not expecting any args to be used inside the program.,so for me there is no use of args[].,that is ....i will run the program w/o any args....like java mainwithoutargs that's it,.......
Hi srinivas, In every main method of any Java program you need to put an String Array as argument. But in your program you does not need to use it. You can have a main like public static void main(String[] args) and may invoke it with java myClass Then you have args.length=0 ::: no problem. For me it is a suprise that main method like public static void main() even compiles?!! Hope this helps. Axel [This message has been edited by Axel Janssen (edited July 06, 2001).]
Hi, The above code works because main is like any other method. We can overload that method. But Java Run Time will look for public static void main(String args[]) for running the application. Try the below code. public class mainwithoutargs { //overloaded method. public static void main() { System.out.println("im in your main w/o string args[]"); } public static void main(String args[]) { System.out.println("im in my main w/o string args[]"); //calling overloaded method. main(); } } Cheers! SriKi.
For me it is a suprise that main method like public static void main() even compiles?!!
Axel, Its nothing to feel surprise. The compiler doesn't complain with the no argument main() method because it thinks that its just an ordinary method.
Note that failure to include a method with the signature public static void main(String[] args) , or its equivalent, does not cause a compile-time error, but you will get a runtime exception because the "main" thread can't find the method it needs to run. [This message has been edited by Scott Appleton (edited July 06, 2001).]
Hi, I think String args[] is compulsary part of main() signature.In many cases we have no need of String array.But it's an essential part which all main() methods have. Regards, Hassan.
Always Belive On Logic!!
srinivas bolloju
Ranch Hand
Joined: Jan 23, 2001
Posts: 112
posted
0
until now it has been a discussion like ...java needs String args[] in the main() for successful execution, but my question is why does it need that....?? but my question is why does it need that....??
but my question is why does it need that....?? but my question is why does it need that....?? Because that is the method signature that the Java VM looks for when it tries to run your program. That is the contract that you must adhere to if you want the JVM to run your program from the command line. When you type the command java MyClass the program you are actually running is the Java Virtual Machine (on DOS/Windows systems, it is the java.exe application). The JVM will then attempt to open and read in a file specified by the first command line parameter which, in the example above, is "MyClass". If it is successful in loading the class file, it will do some initialization and then attempt to invoke a static method main() that takes a single parameter: a String array. If the JVM finds that method signature in the class, it will take the rest of the command line parameters, if there are any, and put them in a String array. If there are no command line parameters other than the class name, then the JVM will have an empty String array. At any rate, that String array is what the JVM passes to the main() static method. That's all it needs to do and if it can't do it because it can't find a static main() method that takes a String array, then it will generate an exception.