calling main method inside main in java
24,908
Solution 1
You can but using the correct format
main(new String[] {"a","b","c"});
should give StackOverFlowError (not tested)
Solution 2
You can. And it is very much legal.
Also, it doesn't necessarily give a StackOverFlowError:
public static void main(String args[]){
int randomNum = (int)(Math.random()*10);
if(randomNum >= 8)
System.exit(0);
System.out.print("Heyhey!");
main(new String[]{"Anakin", "Ahsoka", "Obi-Wan"});
}
Just saying.
Solution 3
You will get StackOverFlowError
. If you call endless recursive calls/deep function recursions.
Thrown when a stack overflow occurs because an application recurses too deeply.
You need to pass String array like
main(new String[] {"a","b","c"});
Solution 4
Kugathasan is right.
Yes it does give StackOverflow Exception. I tested it right now in Eclipse.
class CallingMain
{
public static void main(String[] args)
{
main(new String[] {"a","b","c"});
}
}
I have one suggestion, I think the best way to eliminate the confusion is to try coding and running it. Helps with lots of doubts.
Author by
abc
Updated on July 27, 2020Comments
-
abc almost 4 years
Can we call main method inside main?
public static void main(String[] args) { main({"a","b","c"}); }
Tried to google.Can't find the link. Sorry if the question is trivial
-
Abimaran Kugathasan about 10 yearsIt's StackOverFlowError
-
Dimitar about 7 yearsNo, you cannot! Whereas the compiler won't complain, the recursion you create would exhaust the stack quite fast.
-
Admin about 7 yearsthis code is not for recursion, this is simple code for calling How to call Main method
-
Dimitar about 7 yearsWhat is it when you call main from main?