Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Couldn't understand recursive nature of Java error

 
Vishnu Khera
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • 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?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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 ...
 
Winston Gutkowski
Bartender
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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

 
Campbell Ritchie
Sheriff
Pie
Posts: 48972
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vishnu Khera wrote: . . .
Yes, yes, yes! I like it!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic