• Post Reply Bookmark Topic Watch Topic
  • New Topic

StackOverflow error  RSS feed

 
Mona Kha
Greenhorn
Posts: 7
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hell, I am trying this simple java code that has two objects references t and d which refers to two different objects of same class. The system.out.println statement of t.d is resulting in stackoverflow error. It might I have something to do with circular reference in java that I don't understand. Please explain.

class Test
{
Test d = new Test();

void m2()
{
d = new Test();
}
public static void main(String[] args){
Test t = new Test();
System.out.println(t.d);
}
}

Java>java Test
Exception in thread "main" java.lang.StackOverflowError
        at Test.<init>(Test.java:3)
        at Test.<init>(Test.java:3)
        at Test.<init>(Test.java:3)
        at Test.<init>(Test.java:3)
        at Test.<init>(Test.java:3)
        at Test.<init>(Test.java:3)
        at Test.<init>(Test.java:3)
        at Test.<init>(Test.java:3)
        at Test.<init>(Test.java:3)
        at Test.<init>(Test.java:3)
        at Test.<init>(Test.java:3)
        at Test.<init>(Test.java:3)
        at Test.<init>(Test.java:3)
 
Norm Radder
Rancher
Posts: 2240
28
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Test class creates a new instance of the Test class on line 3 when a new instance of the class is created.

Why?  Remove creating the new instance on line 3.
 
Pete Letkeman
Ranch Foreman
Posts: 917
26
Android Chrome IntelliJ IDE Java MySQL Database
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem lies with  "d = new Test();" which is on line 2 of the following code snippet:


You do have a circular reference. Every time you create a new object reference line 2 executes, which in turn creates a new object reference until you get the error posted. If you replace line 2

with

Then you are in better shape, but you will still need to call the "m2" method otherwise you will get a value of "null" printed because d is initialized to null.

Please use the code tags in this editor when posting code.
 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shruti Khandekar, welcome to the CodeRanch.

A note on your post: it looks like you tried to UseCodeTags (that's a link), thanks for that.  Code goes in between the two tags.  You can check that you did it correctly by using the Preview button, just to the left of the Submit button.  Also, be sure that your code is correctly formatted and indented.
 
O Shea
Ranch Hand
Posts: 200
3
Java MySQL Database Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instance variables are the variables that are associated with objects i.e, for every object there is a separate copy of instance member.

Here in your code instance member is d which is object reference of Test class.

Now when you create object by using

Now an object gets created and for that object its instance should be associated with it and that instance is nothing but this-> . Since it is within the class so it is instance member of class.
Now see -> First you created an object from main method, for that object its instance is associated which is nothing but . This line of code creates another object of Test class and for that newly object its instance also associated with it which is same -> Test d=new Test(); . Hence another object created and for that another object its instance gets associated with it and this goes on like aq cycle hence stack overflows and Error occur at Runtime.
 
Mona Kha
Greenhorn
Posts: 7
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much. That explains the problem very well.
 
O Shea
Ranch Hand
Posts: 200
3
Java MySQL Database Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for awarding cow for me.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!