Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why we pass string object to main?

 
Anuj Prashar
Ranch Hand
Posts: 99
Android Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi..
What is exact reason for which we pass main method string object

public static void main(String [] args)

Thankyou
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They represent any parameters passed to the Java application when it was run.
 
f. nikita thomas
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
imagine you invoke your java program on the commandline:

# java someClass report.txt out.txt

someClass----->your compiled source file minus the .java
report.txt---->[0] of the String[] args
out.txt------->[1] of the String[] args

the arguments are the missing pieces of the program. if you use them in your code and don't present them on the commandline you'll get an
ArrayIndexOutOfBoundsException. take care
[ April 15, 2008: Message edited by: f. nikita thomas ]
 
Anuj Prashar
Ranch Hand
Posts: 99
Android Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for reply....

But if we use empty parenthesis like-
class MyProg {
public static void main(){
}
}
and does not pass any arguement on command line
like--
java MyProg

why I get message - Exception in thread "main" java.lang.NoSuchMethodError:main

Why exception is there, I'm using no parameter in main method so there is no need of passing arguement to it.
Please clear my doubt.
Thanks
 
Freddy Wong
Ranch Hand
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because it's just the rule in Java unlike in some other languages, C/C++/C#
 
Anuj Prashar
Ranch Hand
Posts: 99
Android Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for reply Mr. Wong.

Can you elaborate more on that rule
 
Freddy Wong
Ranch Hand
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In java, the main method must have String[] as its argument. Even if you don't pass any arguments from the comamnd line, you still need it.
 
S Reddy
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is a rule in Java that you have to allow to send Array of String object to main, whether you will pass parameters at run time or not. Even you don't have any need to send parameters through command line you must write main method like

public static void main(String[] a)

you can use any identifier name in the place of a.
 
Anuj Prashar
Ranch Hand
Posts: 99
Android Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for replying
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic