aspose file tools*
The moose likes Beginning Java and the fly likes Doubt regarding StackOverflowError 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 "Doubt regarding StackOverflowError" Watch "Doubt regarding StackOverflowError" New topic
Author

Doubt regarding StackOverflowError

Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Hi

Have a look at the code:



When run, it throws an Error object of type StackOverflowError. Why? What is the recursion that is causing this to happen at run time? The eclipse stack trace points to the line 3. I always held the notion that when a class is loaded in the JVM, the static blocks load first, then instance blocks, then the constructors and finally, the methods. What is going on here?


~ Mansukh
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3656
    
  15
Mansukhdeep Thind wrote:The eclipse stack trace points to the line 3.

When you create a new Test instance you initialise the test variable of that object with another new Test instance. This instance will initialise it's test field with another new Test instance. This instance will initialise it's test field with another new Test instance. This instance will initialise it's test field with another new Test instance. This instance will initialise it's test field with another new Test instance. This instance will initialise it's test field with another new Test instance...


Joanne
Niraj Jha
Ranch Hand

Joined: Feb 20, 2013
Posts: 63

Here instantiation is recursive causing StackOverflowError.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3656
    
  15
Niraj Jha wrote:I executed the same piece of code , no error is coming.

Output:b

With lines 3 and 11 exactly the same as in the OP's code ?
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

@ Jo : Thanks a pile. Again.

@ Niraj : You may have missed out on adding line 3. Are you sure you ran the exact same code which I have pasted above?
Niraj Jha
Ranch Hand

Joined: Feb 20, 2013
Posts: 63

@Mansukhdeep:
It is coming on the same line, earlier I forgot to change on line 3.
Prabhakar Reddy Bokka
Ranch Hand

Joined: Jul 26, 2005
Posts: 193

Joanne Neal wrote:
When you create a new Test instance you initialise the test variable of that object with another new Test instance. This instance will initialise it's test field with another new Test instance. This instance will initialise it's test field with another new Test instance. This instance will initialise it's test field with another new Test instance. This instance will initialise it's test field with another new Test instance. This instance will initialise it's test field with another new Test instance...


Could you explain clearly... what is causing the error??


SCJP 5, SCWCD 5
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

@ Prabhakar : When the main() thread starts its execution in its call stack inside the JVM, the following things happen (in order):

a) The statement invokes the constructor of the class.

b) Remember this, whenever a constructor is invoked, all the instance variables of the class are assigned their respective values(either default or through setters and getters) for this new object on the heap.

c) As a part of action in statement (b) above, since this instance variable assignment at line 3 is itself a part of the class's instance variables, it will again invoke the constructor of the class.

d) This second invocation of the constructor again tries to do the action mentioned in (c ) above. This is where the memory gets screwed up. This action will never complete since the instance variable assignment is calling the same constructor which initiated that assignment. And so the stack overflows its capacity.

Let me know in case you have any further doubts.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
Get a pencil, paper and eraser. Start from the new Foo(); call in line 12, and write down how many Foo objects there are.
Hint: Every new Foo() counts as 1.
Prabhakar Reddy Bokka
Ranch Hand

Joined: Jul 26, 2005
Posts: 193

@ Mansukhdeep: Thank you !
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt regarding StackOverflowError