Forums Register Login

Why we pass string object to main?

+Pie Number of slices to send: Send
Hi..
What is exact reason for which we pass main method string object

public static void main(String [] args)

Thankyou
+Pie Number of slices to send: Send
They represent any parameters passed to the Java application when it was run.
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
Because it's just the rule in Java unlike in some other languages, C/C++/C#
+Pie Number of slices to send: Send
Thanks for reply Mr. Wong.

Can you elaborate more on that rule
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Thanks for replying
Space pants. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 2537 times.
Similar Threads
creation of object for a class in static method
pass by value & ref
Runtime Function calling -- code
Exception doubt.
doubt regarding the method call
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 03:11:36.