Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception in thread "main" java.lang.StackOverflowError

 
suresh midde
Greenhorn
Posts: 25
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Mark Vedder
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Campbell Ritchie
Sheriff
Posts: 48441
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Sheriff
Posts: 48441
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great minds think alike . . . but welcome to JavaRanch
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic