Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

other situation for StackOverFlowError.

 
Ritesh raushan
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

in which situtation StackOverFlowError will come.

1-in recursion code,but i want to know other situtation where StackOverFlowError will come.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12122
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suppose if method A calls method B which calls method A...or any number of circular calls would do it.

Or if the constructor of an object tries to create an instance of itself...something like



note: it's early, and I haven't had coffee yet, so the above code may not be 100% correct
 
Matthew Brown
Bartender
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You get a StackOverflowError whenever the stack gets too "deep". Every time a method is called that adds an entry to the stack, which is removed when that method returns. There's a limited amount of memory allocated to this stack, so if you nest method calls too deep then this can happen.

In practice, this limit is pretty large (hundreds or thousands of calls, depending on the memory allocated). You could reach the limit non-recursively if you really wanted to and had a lot of patience. But in practice some form of recursive loop will nearly always be the cause.
 
Nikhil Sagar
Ranch Hand
Posts: 216
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ritesh raushan wrote:
in which situtation StackOverFlowError will come.

1-in recursion code,but i want to know other situtation where StackOverFlowError will come.


Look at the name of this error it is, StackOverFlowError, now what do you think about its perfect name ??
when ever
"Stack Over Flows"
, a StackOverFlowError comes.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic