• Post Reply Bookmark Topic Watch Topic
  • New Topic

StackOverflowError when creating an object  RSS feed

 
Eric Longhorn
Ranch Hand
Posts: 40
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So this code throws a StackOverflowError:

But I don't understand how. I get that an instance is created off of class Emp, which is the first step that causes the error, however after that I'm totally lost.
Could someone point me in the right direction?
Thanks.
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A call to the Emp constructor causes a call to the Emp constructor, causes a call to the Emp constructor, causes .....
 
Eric Longhorn
Ranch Hand
Posts: 40
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How does the Emp constructor call itself over and over again?
 
Norm Radder
Rancher
Posts: 2240
28
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The new statement on line 2.
 
Eric Longhorn
Ranch Hand
Posts: 40
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I get that the default constructor made from the new on line 2 would like this:


Or something like that. What I don't get is how the new Emp(); is in the default constructor.
Prior to this question, I had thought that default constructors were either empty, or had super() in the case of inheritance, but clearly this is wrong.
How is an object in the default constructor?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A default constructor is empty. What makes you feel the need to perform a new in the constructor? That is, as you have seen, not useful and just creates an infinite loop.

P.S. When you explicitly declare a constructor it is, by definition, not a default constructor. A default constructor is the one that is automatically created when no other constructor is declared.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your class Emp has a member variable which is an Emp.

So, what happens when you create a new Emp object?

The member variable 'emp' will be initialized, by creating a second new Emp object.
Note that that second Emp object itself again has a member variable 'emp', which will be initialized by creating a third new Emp object.
And that third Emp object again has a member variable 'emp', which will be initialized by creating a fourth new Emp object.
And that fourth Emp object again has a member variable 'emp', which will be initialized by creating a fifth new Emp object.
And that fifth Emp object again has a member variable 'emp', which will be initialized by creating a sixth new Emp object.
And that sixth Emp object again has a member variable 'emp', which will be initialized by creating a seventh new Emp object.
...

Etcetera, until the stack overflows.
 
Eric Longhorn
Ranch Hand
Posts: 40
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I get the general gist of it, but I'm stuck on how the second object in class Emp has a member variable emp.
 
Knute Snortum
Sheriff
Posts: 4279
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, with the code you posted, it would be a member variable mgr of type Emp, but it's the same problem: you do not need to (and in fact, you can't) make a new anything of type Emp in the Emp constructor. 
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eric Longhorn wrote:Ok, I get the general gist of it, but I'm stuck on how the second object in class Emp has a member variable emp.

Sorry, I meant a member variable named 'mgr', not named 'emp'.

Every Emp object has a member variable named 'mgr' (remember, each object has its own copy of the member variables). When you create an Emp object, that Emp object is, during its initialization phase, going to create another Emp object, which in turn is going to create another Emp object while it's being initialized, etc. You have an infinite recursive loop there.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!