This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why am i getting StackOverflowError?

 
Vinay Belagavi
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


When I compile and run the program, I get a StackOverflowError.I am not able to understand the reason.

Is it because I have created an instance of the class in the constructor?The program works fine if line 6 and 7 are removed.

The documentation says that
stack overflow occurs because an application recurses too deeply
.
what do you mean by application recurses too deeply?

Can anybody explain this to me.

I am breaking my head over this
 
Simeon Cherniy
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You call:
InstanceTest it = new InstanceTest(); -> constructor called
InstanceTest it1 = new InstanceTest(); -> constructor called
...
Recursion.
Too deeply means that ideally this recursion is infinite
 
Watsh Rajneesh
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stack overflow happens as you are calling the constructor (new InstanceTest()) within the constructor body - and this makes it a recursive function.

The function calls are stored on the stack of the process (jvm's process' stack memory). When a function is called, its return address and local vars/references are kept on the top of the stack. If this function calls another function (or itself - as in recursion) then the stack size grows to accomodate the new function's info. When there is no exit criteria specified for a recursion (as in your case) the functions keep adding to the top of the stack and there comes a time when stack size reaches the maximum possible size and the JVM process balks out complaining an overflow.
 
Vinay Belagavi
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Simeon and Rajneesh,

Itz all clear now
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic