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

Doubt regarding StackOverflowError

 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Niraj Jha
Ranch Hand
Posts: 63
Java Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here instantiation is recursive causing StackOverflowError.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ 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
Posts: 63
Java Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Mansukhdeep:
It is coming on the same line, earlier I forgot to change on line 3.
 
Prabhakar Reddy Bokka
Ranch Hand
Posts: 197
Java Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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??
 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ 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
Pie
Posts: 48968
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 197
Java Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ Mansukhdeep: Thank you !
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic