package com.object2;class Test1 {
private static Test1 t4 = new Test1(4);
private Test1 t5 = new Test1(5);//加上这句话报错
//java.lang.StackOverflowError
//at com.object2.Test1.<init>(Test.java:8)
Test1(int index) {
System.out.println("Test" + index);
}
}public class Test extends Test1 {
private static Test1 t1 = new Test1(1);
private Test1 t2 = new Test1(2);
Test(int i) {
super(i);
System.out.println("Test" + i);
}
public static void main(String[] args) {
Test t3 = new Test(3);
}
}
private static Test1 t4 = new Test1(4);
private Test1 t5 = new Test1(5);//加上这句话报错
//java.lang.StackOverflowError
//at com.object2.Test1.<init>(Test.java:8)
Test1(int index) {
System.out.println("Test" + index);
}
}public class Test extends Test1 {
private static Test1 t1 = new Test1(1);
private Test1 t2 = new Test1(2);
Test(int i) {
super(i);
System.out.println("Test" + i);
}
public static void main(String[] args) {
Test t3 = new Test(3);
}
}
放到构造函数里面去实例化。
private static Test1 t4 = new Test1(4);
这个之所以可以,因为他是静态的,不需要实例化。---
private Test1 t5 = new Test1(5);//循环实例化,是个死循环导致堆栈溢出。