It can be compiled without an argument - Maneesh's example just proves it.
But it won't be recognised by the JVM as a starting point for a program. And that's simply because the JVM is looking for a method public static void main(String). Nothing else will do - that's just how the JVM is written.
As to why - the argument is there so that command-line arguments can be passed into the program.
And what command did you type at the command line to cause that error to appear?
posted 7 years ago
Ah, that's not a compiler error. That's a run-time error - you must be doing something that is attempting to run the class, rather than just compiling it. In which case, the error is because of the reasons previously explained.
Methods have their own method signature.
And method signature = method name + method argument types.
JVM to run the main method as a entry point.
If you remove the type parameter so that JVM to can not find main method.
So you can compiled it without any errors ,but you can't run it.
P.S.My English is poor，if you find any grammar mistakes, please tell me to correct it。
Jitendra Jha wrote:
Why did they(creators of java) made it compulsory for main to have an argument.....
What advantage does it(making it compulsory) give us.
Even if she did not ask it,this question has perflexed me too, so kindly help me understand it.
Fred has already answered it above
Fred wrote:That's because when you try and run a class, the JVM explicitly looks for a method with the signiature of "public static void main (String  args)".
Maneesh Godbole wrote:Fred has already answered it above
Well, Jitendra's right that they could have designed it in a different way.
I think the answer is probably that it's simpler, because:
- There's only one valid way of declaring the main method.
- If more than one way of writing main() is valid, what happens if you overload it? The JVM wouldn't know which to run. OK, it could run one if command-line arguments are given, and the other otherwise, but that's just going to confuse people.
- If you wrote a main() with no arguments, and then tried to pass in command-line arguments it wouldn't work. This will also confuse people.
So keep it simple - having to add a String even if you don't use it is hardly difficult.
[Edited to fix the override/overload "deliberate mistake" ]
posted 7 years ago
Thanks Matthew for that reasoning.
We of course cannot be sure as to what exactly was their reason but the way you have put it is probably the right way to think about it.
Those who dance are thought mad by those who hear not the music. This tiny ad plays the bagpipes:
Create Edit Print & Convert PDF Using Free API with Java