Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why does this create a stack overflow error?

 
Ted North
Ranch Hand
Posts: 201
1
Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


a.Foo1 class the above class, b.Foo2 refers to...
 
E Armitage
Rancher
Posts: 989
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Creating a Foo2 causes another Foo2 to be created which causes another Foo2 to be created therefore creating another Foo2 which then causes a Foo2 to ...
 
Ted North
Ranch Hand
Posts: 201
1
Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
E Armitage wrote:Creating a Foo2 causes another Foo2 to be created which causes another Foo2 to be created therefore creating another Foo2 which then causes a Foo2 to ...


Armitage,

How do I prevent this from happening? It seems like the code would create a new Foo2 then create another single Foo2 object but instead it just keeps creating them until the java.lang.stackoverflowerror
I need to punch deck to see if I can figure out what is going wrong. This is a strange error I ran into once before. I cannot exactly wrap my head around the logic...

Actually, I think I can visualize this now. A new Foo2 is created, this object also calls for the instantiation of a new Foo2, and so on as you illustrated. I cannot believe creating a simple instance member causes a stackoverflow error. hahah :/

Respectfully,

Ted

 
E Armitage
Rancher
Posts: 989
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ted North wrote:
Actually, I think I can visualize this now. A new Foo2 is created, this object also calls for the instantiation of a new Foo2, and so on as you illustrated. I cannot believe creating a simple instance member causes a stackoverflow error. hahah :/

Respectfully,

Ted

Yes, the first suspect is having a Foo2 inside Foo2. It's not inherently bad to do that because there are scenarios that require such modeling but one should become extra alert after seeing that. Then the immediate instantiation of the Foo2 in the intializers section just gives the whole thing away. To successfully use a Foo2 inside a Foo2 one has to avoid initializing a Foo2 when a Foo2 is created.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Little pop quiz: what happens if you replace the line Foo2 ff = new Foo2(); with Foo1 ff = new Foo1();? You should of course try to answer the question without running the code...
 
Ted North
Ranch Hand
Posts: 201
1
Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
E Armitage wrote:
Ted North wrote:
Actually, I think I can visualize this now. A new Foo2 is created, this object also calls for the instantiation of a new Foo2, and so on as you illustrated. I cannot believe creating a simple instance member causes a stackoverflow error. hahah :/

Respectfully,

Ted

Yes, the first suspect is having a Foo2 inside Foo2. It's not inherently bad to do that because there are scenarios that require such modeling but one should become extra alert after seeing that. Then the immediate instantiation of the Foo2 in the intializers section just gives the whole thing away. To successfully use a Foo2 inside a Foo2 one has to avoid initializing a Foo2 when a Foo2 is created.


Armitage,

Thank-you for more feedback on this. The problem is as you are describing some type of recursive call that results when creating a new Foo2 object in main() and then instantiating a new Foo2 in the instance member field.

Roel De Nijs wrote:
Little pop quiz: what happens if you replace the line Foo2 ff = new Foo2(); with Foo1 ff = new Foo1();? You should of course try to answer the question without running the code...



Roel,

I tried to visualize this in my mind but could only take guesses... It seems like it could go either way, creating a new Foo1 inside a Foo2 seems, based on my relatively lack of experience with Java, could create a java.lang.StackOverflowError (SOE) but upon running this it does not. I also tried having a Foo1 reference variable that polymorphically points to a Foo2 object. This also causes a SOE. I guess it is the recursiveness of instantiating a new Foo2 object and then creating one as an instance member.

Thank-you for your help Roel and Armitage.

Regards,

Ted
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic