aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Why does this give a stackoverflowerror? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Why does this give a stackoverflowerror?" Watch "Why does this give a stackoverflowerror?" New topic
Author

Why does this give a stackoverflowerror?

Oceana Wickramasinghe
Ranch Hand

Joined: Mar 02, 2011
Posts: 77


Thanks in advance
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
Because each instantiation of A causes another instantiation of A, thus causing an endless succession of A object creations. Try what happens if you use "static A a = new A();" instead of the current line 2.
Oceana Wickramasinghe
Ranch Hand

Joined: Mar 02, 2011
Posts: 77
Oh,I thought it would only create a reference variable, not the object. Another question, isnt the stackoverflow error related to the stack, but objects are created on heap correct? So why does it result in a stack overflow?
Norbert Muench
Greenhorn

Joined: Mar 09, 2012
Posts: 19
Yes, objects are created on the heap. The stack overflow is causes by the nested calls to A's constructor, which will eventually overflow the stack long before the A objects you create exhaust the heap.
Line 2 of your code will essentially cause the constructor of class A to call itself.
Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
Tim Moores wrote:Because each instantiation of A causes another instantiation of A, thus causing an endless succession of A object creations. Try what happens if you use "static A a = new A();" instead of the current line 2.


So, if a is a static instance, will the new instance gets created recursively?
The static instance is a class level instance, not related to any object instance. How can we guarantee that this static instance won't be created once it is created once?
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
Static variables are by definition created once for each class, in contrast to instance variables that are created once for each object. So it doesn't mater how many objects are created - only one copy of the static variable exists.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4464
    
    8

Oceana Wickramasinghe wrote:Oh,I thought it would only create a reference variable, not the object.


This would declare a reference variable:
A a;

Whereas this declares a reference variable and creates a new object.
A a=new A();

new always results in a new object being created.

Oceana Wickramasinghe wrote:Another question, isnt the stackoverflow error related to the stack, but objects are created on heap correct? So why does it result in a stack overflow?

The word "stack" also refers to the execution stack, which keeps track of which methods are called. Everytime a method is called, then that call is added to the stack, because the program needs to know where to return to when the method has completed. There's a limited amount of memory for this, and when it runs out you get a "stack overflow". So this error tends to be caused by a recursive method calls that doesn't have an exit condition. It's not really the memory running out because there are too many objects that's causing the error (that would give you an OutOfMemoryError), it's the stack overflowing because the A constructor is effectively calling itself.
Oceana Wickramasinghe
Ranch Hand

Joined: Mar 02, 2011
Posts: 77
Helen Ma wrote:
So, if a is a static instance, will the new instance gets created recursively?
The static instance is a class level instance, not related to any object instance. How can we guarantee that this static instance won't be created once it is created once?


Ok this confused me even more, i tried the following.


Ok so no stackoverflow error here, if im not mistaken it the reference variable that becomes static, not new A();, i mean objects cant be static right?

So if it creates endless new A() s in my previous program why doesnt the same thing happen here, there will not be endless 'a' s since its static, but what about the object 'a' refers to.
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
Because of this:
Tim Moores wrote:Static variables are by definition created once for each class, in contrast to instance variables that are created once for each object. So it doesn't mater how many objects are created - only one copy of the static variable exists.
Oceana Wickramasinghe
Ranch Hand

Joined: Mar 02, 2011
Posts: 77
so new A(); becomes static too, not only its reference?
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
"static" applies to object references (a.k.a variables), not to objects.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4464
    
    8

The important thing is when that is executed. If it's static, it's executed once - when the class is loaded. If it's not static it's executed every time an instance is created. So only when it's not static do you go into an infinite recursion.
Oceana Wickramasinghe
Ranch Hand

Joined: Mar 02, 2011
Posts: 77
Matthew Brown wrote:The important thing is when that is executed. If it's static, it's executed once - when the class is loaded. If it's not static it's executed every time an instance is created. So only when it's not static do you go into an infinite recursion.


You mean the 'line' where static is declared?
Oceana Wickramasinghe
Ranch Hand

Joined: Mar 02, 2011
Posts: 77
Tim Moores wrote:"static" applies to object references (a.k.a variables), not to objects.


Then shouldnt there be an infinite number of objects like in the previous example. You're saying that the object can not be static, only the reference, then how does that stop the stackoverflowerror, since multiple objects is what causes stackoverflowerror not references, i dont understand.
Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
In this example, objects of instance A are created recursively and finally causes stack over flow error.
If you declare object as as a static instance, once this class level object is created, it won't be created again. Therefore, no stack over flow error is caused.

By the way, does this static object a singleton?
Oceana Wickramasinghe
Ranch Hand

Joined: Mar 02, 2011
Posts: 77
Since a object is treated as an instance variable for aa, does it get created inside the aa object(object within object infinite times) or at different locations on the heap?
Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
I think each object aa, aa(1), aa(2).... will be put in different locations of the heap.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Why does this give a stackoverflowerror?