• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Doubt regarding StackOverflowError

 
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Ranch Hand
Posts: 63
Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here instantiation is recursive causing StackOverflowError.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Mansukhdeep:
It is coming on the same line, earlier I forgot to change on line 3.
 
Ranch Hand
Posts: 198
Oracle Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79716
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 198
Oracle Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@ Mansukhdeep: Thank you !
 
I’m tired of walking, and will rest for a minute and grow some wheels. This is the promise of this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic