File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Couldn't understand recursive nature of Java error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Couldn Watch "Couldn New topic
Author

Couldn't understand recursive nature of Java error

Vishnu Khera
Greenhorn

Joined: Nov 29, 2008
Posts: 24





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

Joined: Aug 05, 2005
Posts: 3646
    
  15
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 ...


Joanne
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Vishnu Khera
Greenhorn

Joined: Nov 29, 2008
Posts: 24

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

Joined: Oct 13, 2005
Posts: 39393
    
  28
Vishnu Khera wrote: . . .
Yes, yes, yes! I like it!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Couldn't understand recursive nature of Java error