Can somebody tell me how each code run and how one of them lead to stackoverflow error ?
Raj Kamal
Ranch Hand
Joined: Mar 02, 2005
Posts: 392
posted
0
Hi,
In the first code there is an instance variable 't' of the same type. This leads to a cyclic creation of objects, hence the StackOverFlow. Whereas in the second the 'static' modifier makes the property at the class level i.e. There is only one reference which is shared by all objects of the class and hence there is no repetitive object creation. So a StackOverFlow does not arise with this case. Hope I am clear enough with my explanation?
Cheers,
Raj.
Brij Garg
Ranch Hand
Joined: Apr 29, 2008
Posts: 234
posted
0
public class MyApp {
MyApp t = new MyApp();
public static void main(String[] args) {
//statement1// new MyApp();
}
}
Above given code should give StackOverFlow.
every MyApp object has an instance variable t which is refering to another MyApp object.
When this class will execute then statement at line 1 will execute and it leads to the creation of never ending MyApp objects until StackOverFlow.
Because in first case you are recursively creating an object of MyApp class. Line #2
Whenever you will create a new object of MyApp class it will execute the line # 2 and again will create another instance of MyApp and again and again till the memory will not be flooded with MyApp objects,
But in second case it will not happen because here you have declared the MyApp object as static and there will be only one object for this class member (MyApp at line 2). A new object will be created only once the class will be loaded first time so no recursive call which could lead to StackOverflowError.
This message was edited 1 time. Last update was at by Harpreet Singh janda
kevin saber
Greenhorn
Joined: Mar 07, 2010
Posts: 21
posted
0
still a little abstract... But i will remember it .
Lorand Komaromi
Ranch Hand
Joined: Oct 08, 2009
Posts: 276
posted
0
kevin saber wrote:But i will remember it .
You don't need to remember the exact explanation, you just need to understand the basics, e.g. how class members are initialized and that endless (or long enough) recursion causes StackOverFlow...
Well, let's look at what happens exactly when you run your first code example.
1. Line 5: you create a new MyApp() object.
2. As part of the initialisation of that new object, line 2 is run.
3. Line 2: you create a new MyApp() object.
4. As part of the initialisation of that new object, line 2 is run.
5. Line 2: you create a new MyApp() object.
6. As part of the initialisation of that new object, line 2 is run.
7. Line 2: you create a new MyApp() object.
8. As part of the initialisation of that new object, line 2 is run.
9. ...
10000000. There is no memory available on the stack anymore and you get a StackOverflowError.
You are trying to create a MyApp object, which contains a MyApp object, which also contains a MyApp object, ...
You have an infinite recursive loop.
This message was edited 1 time. Last update was at by Jesper Young