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" ?
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 ]
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
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
Joined: Oct 13, 2005
Great minds think alike . . . but welcome to JavaRanch
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com
subject: Exception in thread "main" java.lang.StackOverflowError