• 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

Couldn't understand recursive nature of Java error

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator





I tried the above code without try-catch block - the output is ||| at StackError.<init>(TestStackError.java:3) ||| and then hundreds of lines of ||| at StackError.<init>(TestStackError.java:7) ||| If I don't print the stack trace stackoverflow error is caught.

What I do understand is: all classes are loaded at runtime. Once we try to create an object in main() - as per Java Language Specification '12.5 Creation of New Class Instances' variable "se" of type "StackError" is initialized before object "obj" is formed. This results to an error, why I could not understand? Why the error is recursive, I'm not able to fully understand?
 
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vishnu Khera wrote:What I do understand is: all classes are loaded at runtime. Once we try to create an object in main() - as per Java Language Specification '12.5 Creation of New Class Instances' variable "se" of type "StackError" is initialized before object "obj" is formed.


And what do you initialise the se variable to - a StackError object and when that object is created it will contain an se variable that points to another StackError object and when that object is created it will contain an se variable that points to another StackError object and when that object is created it will contain an se variable that points to another StackError object and when that object is created it will contain an se variable that points to another StackError object and when that object is created it will contain an se variable that points to another StackError object and when that object is created it will contain an se variable that points to another StackError object and when that object is created it will contain an se variable that points to another StackError object and when that object is created it will contain an se variable that points to another StackError object and when that object is created it will contain an se variable that points to another StackError object ...
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vishnu Khera wrote:why I could not understand? Why the error is recursive, I'm not able to fully understand?


Well, think it through. You're asking the JVM to create a new StackError object, but in order to do that, it needs to create a new StackError object, which in turn needs to create a new StackError object... which is basically an endless loop, so eventually it breaks.

If it's recursion you're asking about, then the best explanation I can give you is the following quote:

Someone wise and witty once wrote:To understand recursion, you must first understand recursion...


Think about it, because that's basically how it works.

Winston
 
Vishnu Khera
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thanks Winston and Joanne appreciate the quick response

Yesss! For a class my example gives infinite recursion as is the example below for a method

 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vishnu Khera wrote: . . .

Yes, yes, yes! I like it!
 
I promise I will be the best, most loyal friend ever! All for this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic