• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why am i getting StackOverflowError?

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Simeon and Rajneesh,

Itz all clear now
 
We cannot change unless we survive, but we will not survive unless we change. Evolving tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic