aspose file tools*
The moose likes Beginning Java and the fly likes StackOverflowException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "StackOverflowException" Watch "StackOverflowException" New topic
Author

StackOverflowException

Shaveta Chawla
Greenhorn

Joined: Aug 01, 2007
Posts: 13
Hi
Please tell me why theres stack overflow exception in the program below:

class A{
A a=new A();
int i=9;

}

public class finaltest {

public static void main(String[] args) {
A aa=new A();
A aa2=new A();
aa2=aa.a;
aa2.i=10;
System.out.println(aa2.i);

}
}


Thanks
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Originally posted by Shaveta Chawla:
class A{
A a=new A();
int i=9;

}


I haven't tested this, but when you create an instance of A, one of its fields creates an instance of A. When you create an instance of A, one of its fields creates an instance of A. When you create an instance of A, one of its fields creates an instance of A. When you create an instance of A, one of its fields creates an instance of A. When you create an instance of A, one of its fields creates an instance of A. When you create an instance of A, one of its fields creates an instance of A.

etc.
saurav sarkar
Ranch Hand

Joined: Jan 07, 2007
Posts: 180

the memory is unable to hold the number of objects
you are creating.You have put the JVM in a loop
by creating objects.

you are doing this

A aa=new A();

then again inside A you are doing A a new A();


Be Objectively Oriented.Explore the power of OOPs.
My Blog, Eclipse EMF Query committer.
Ashwin Sharma
Greenhorn

Joined: Nov 03, 2006
Posts: 28
hi....
if you want to use it make instance variable in class A static. Then the code will work.

class A{
A a=new A();
int i=9;
}
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

What you mean is that the code will no longer have a StackOverflow, but is that what the code is supposed to do?
 
Don't get me started about those stupid light bulbs.
 
subject: StackOverflowException