把第5行代码改为Outer2 a = new Outer2() ;程序报错:Exception in thread "main" java.lang.StackOverflowError;在本类中要实例化该对象,必须使用this替代吗?
package comm.test;
class Outer2 {
private String title="HELLo world";
Outer2 a = this; //第5行
public void fun() {
new Inner2(a).print();
}
public String getTitle() {
return new Outer2().title;
}
}
class Inner2 {
private Outer2 outer;
public Inner2(Outer2 out) {
this.outer= out;
}
public void print() {
System.out.println("书名:"+this.outer.getTitle());
}
}
public class Test14 {
public static void main(String[] args) {
Outer2 out = new Outer2();
out.fun();
}
}