Forums Register Login

Exception in thread "main" java.lang.StackOverflowError

1
+Pie Number of slices to send: Send
Hi ,
When I tried to execute the following code, I got an error which is presented down.

public class Test {

public static void main(String... strings ){

System.out.println("calling main 1");
main();
}

}

It got compiled well, but while executing gave following error.


calling main 1
calling main 1
calling main 1
Exception in thread "main" java.lang.StackOverflowError
at sun.nio.cs.SingleByteEncoder.encodeArrayLoop(Unknown Source)
at sun.nio.cs.SingleByteEncoder.encodeLoop(Unknown Source)
at java.nio.charset.CharsetEncoder.encode(Unknown Source)
at sun.nio.cs.StreamEncoder.implWrite(Unknown Source)
at sun.nio.cs.StreamEncoder.write(Unknown Source)
at java.io.OutputStreamWriter.write(Unknown Source)
at java.io.BufferedWriter.flushBuffer(Unknown Source)
at java.io.PrintStream.write(Unknown Source)
at java.io.PrintStream.print(Unknown Source)
at java.io.PrintStream.println(Unknown Source)
at com.cm.portlet.trip.indent.action.Test.main(Test.java:7)
at com.cm.portlet.trip.indent.action.Test.main(Test.java:8)
at com.cm.portlet.trip.indent.action.Test.main(Test.java:8)

Since I have not defined any no-arg method for main(), I assume, it never tries to call main(). But why this was printing "calling main 1" ?

Can anyone let me understand this?


Regards
Suresh
[ August 22, 2008: Message edited by: suresh midde ]
+Pie Number of slices to send: Send
Because you are using a varargs definition for your main, that method can take anywhere from 0 to an unlimited number of arguments. So the call to main() with no arguments satisfies that. So the method is called recursively forever, and you get the stackOverflowError. If you change the String... vararg to an array String[], then your code would not compile since you do not have a main() (with no arguments) method. You'd have to change that line to main(null). (But you would still get the StackOverflowError since you would have an infinite recursive loop - that is the main method keeps calling itself forever.)
[ August 22, 2008: Message edited by: Mark Vedder ]
+Pie Number of slices to send: Send
I was surprised that it compiled, but it is because of your unusual use of the ... operator. You usually get that sort of Exception when you have an infinite recursion, which is exactly what you get here. If you used the conventional signature main(String[]) it wouldn't compile.

You can pass nothing to ... but not to String[].

You are calling main with no arguments, then you call main with no arguments thne you call main with no arguments then you call main with no etc etc
+Pie Number of slices to send: Send
 

Originally posted by suresh midde:
Since I have not defined any no-arg method for main(), I assume, it never tries to call main().



You've defined your main method using the varargs syntax. That means it is expecting zero or more Strings to be passed to it. A call to main() with no parameters is therefore actually calling your defined main method., so you are recursing (calling a method from itself) - hence the stack overflow
+Pie Number of slices to send: Send
Great minds think alike . . . but welcome to JavaRanch
crispy bacon. crispy tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 11251 times.
Similar Threads
preparedstatement
StackOverflowError with 'this'
Object Creation and Construction
NoClassDefFound-wrong name
PATH & CLASSPATH?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 02:38:15.