csdn_flying97
2015-11-17 03:13
采纳率: 50%
浏览 1.4k
已采纳

java代码编程报错,求原因。

把第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();
}

}

5条回答 默认 最新

相关推荐 更多相似问题