Behaviour of main() method
posted 12 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Hello Folks,
I just need explanation of this below simple code.
This code compiles fine but when I run it it throws following error
D:\Java\test>java MainTest
Exception in thread "main" java.lang.NoSuchMethodError: main
It says that JVM is not able to recognize this main method which is quite obvious because internally the line
is converted into
Hence my question is why compiler didn't recognize it ?
Help in this matter is highly appreciated.
Thanks...
I just need explanation of this below simple code.
This code compiles fine but when I run it it throws following error
D:\Java\test>java MainTest
Exception in thread "main" java.lang.NoSuchMethodError: main
It says that JVM is not able to recognize this main method which is quite obvious because internally the line
is converted into
Hence my question is why compiler didn't recognize it ?
Help in this matter is highly appreciated.
Thanks...
OCJP 6
posted 12 years ago
Your compiler recognised it just fine, which is why you didn't get any errors.
Question: What is the signature of the main() method? If need be, go back to your book, or the tutorials, and check.
Winston
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Khuzema Dharwala wrote:Hence my question is why compiler didn't recognize it ?
Your compiler recognised it just fine, which is why you didn't get any errors.
Question: What is the signature of the main() method? If need be, go back to your book, or the tutorials, and check.
Winston
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
posted 12 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
The compiler didn't complain because that's an entirely valid method signature. You're allowed to have an argument which takes an array of arrays. But that doesn't mean it's a main method that will be recognised by the JVM as a valid start point.
You can use main(String[] args), or you can use main(String... args) (which compiles to the same thing). But main(String[]... args) is something different, as you've seen.
You can use main(String[] args), or you can use main(String... args) (which compiles to the same thing). But main(String[]... args) is something different, as you've seen.
posted 12 years ago
Thanks Winston for your reply what I actually mean was why compiler didn't recognized that it is not a valid main method that JVM will not accept.
Thanks Matthew for your explanation I wasn't aware of the fact that main method is allowed to take array of arrays.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Thanks Winston for your reply what I actually mean was why compiler didn't recognized that it is not a valid main method that JVM will not accept.
Thanks Matthew for your explanation I wasn't aware of the fact that main method is allowed to take array of arrays.
OCJP 6
posted 12 years ago
Erm...what he actually said was that the main method is NOT allowed to take an array of arrays - at least, not if you want it to be recognised by the JVM. You can overload any method; but only one signature of main() is recognised by the java command.
Winston
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Khuzema Dharwala wrote:Thanks Matthew for your explanation I wasn't aware of the fact that main method is allowed to take array of arrays.
Erm...what he actually said was that the main method is NOT allowed to take an array of arrays - at least, not if you want it to be recognised by the JVM. You can overload any method; but only one signature of main() is recognised by the java command.
Winston
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
posted 12 years ago
How would the compiler know that you don't have another method that calls this method? Are you saying that because the method is call main(), the compiler should assume that you will actually use it as a JVM main method? It is perfectly valid to have classes without main methods, which is what you have here.
Henry
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Khuzema Dharwala wrote:
Thanks Winston for your reply what I actually mean was why compiler didn't recognized that it is not a valid main method that JVM will not accept.
How would the compiler know that you don't have another method that calls this method? Are you saying that because the method is call main(), the compiler should assume that you will actually use it as a JVM main method? It is perfectly valid to have classes without main methods, which is what you have here.
Henry
posted 12 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Valid point Henry I actually thought that "because the method is call main(), the compiler should assume that you will actually use it as a JVM main method?" but my doubt is clear now thanks much appreciated.
OCJP 6
| We noticed he had no friends. So we gave him this tiny ad: Paul Wheaton's 16th Kickstarter: Gardening playing cards for gardeners and homesteaders https://coderanch.com/t/889615/Paul-Wheaton-Kickstarter-Gardening-playing |











