This code shows error at run time:
class Animal {
Animal object1 = new Animal();
public static void main(String[] args) {
Animal obj = new Animal();
}
}
This is slightly different code with Animal obj = new Animal();
this line from main
method commented out. this code shows no run time error.
class Animal {
Animal object1 = new Animal();
public static void main(String[] args) {
// Animal obj = new Animal();
}
}
How is this caused and how can I solve it? I am using command prompt to run this code.
If you have a member variable which is initialized to an instance of the same class, then when that instance is created, it will also have a member variable which is initialized to an instance of the same class, and when that instance is created, it will also have a member variable which is initialized to an instance of the same class, and when that instance is created, it will also have a member variable which is initialized to an instance of the same class, and when that instance is created, it will also have a member variable which is initialized to an instance of the same class, and when that instance is created, it will also have a member variable which is initialized to an instance of the same class, and when that instance is created, it will also have a member variable which is initialized to an instance of the same class, and when that instance is created, it will also have a member variable which is initialized to an instance of the same class, and when that instance is created, it will also have a member variable which is initialized to an instance of the same class, and when that instance is created, it will also have a member variable which is initialized to an instance of the same class, and when that instance is created, it will also have a member variable which is initialized to an instance of the same class, and when that instance is created, it will also have a member variable which is initialized to an instance of the same class, and when that instance is created, it will also have a member variable which is initialized to an instance of the same class, and when that instance is created, it will also have a member variable which is initialized to an instance of the same class, and when that instance is created, it will also have a member variable which is initialized to an instance of the same class, and when that instance is created, it will also have a member variable which is initialized to an instance of the same class...
and then the stack will overflow and it will stop. It's OK for an object to have a pointer to another instance of the same class as a member, but it's not OK to create that instance in the constructor, or have it initialized in the body of the class, or you'lll recursively create objects until your stack overflows. Normally if you want such a member variable, then you accept the object as a constructor argument.
Short answer infinite recursion.
Long answer if you want recursive data structures like that, you can do something like this:
public class A {
A object1;
public A(A member) {
this.object1 = member;
}
public static void main(String[] args) {
A item = new A(new A(null)); // note the base case/termination of the recursion!
}
}
Or:
public class B {
B object1;
public void init() {
object1 = new B();
}
public static void main(String[] args) {
B item = new B();
item.init();
// now item.object1 != null, but item.object1.object1 == null
}
}
In either case, you will have “sentinel” or “leaf” nodes in your data structure, which point to a null
value.
It's a stack overflow.
It's similar to calling a function from the same function, like this:
void func(){
func();
}
It will repeat until the stack fills, and then the program will crash.
Cheers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With