• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

main without string args[ ]

 
srinivas bolloju
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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[]");
}
}
 
Anshul Manisha
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
srinivas bolloju
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,.......
 
Axel Janssen
Ranch Hand
Posts: 2166
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
krishna avrs
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
rani bedi
Ranch Hand
Posts: 358
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Scott Appleton
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you don't supply any parameters then args is simply an empty array.
------------------
Co-Moderator of the Programmer Certification Forums
 
Hassan Naqvi
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
srinivas bolloju
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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....??
 
Junilu Lacar
Bartender
Pie
Posts: 7749
61
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
srinivas bolloju
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i got it..thanx lacar
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic