我正在研究内部类概念并编写以下代码_
public class InnerClassConcepts {
private int x = 11;
public static void main(String... args) {
// method local 'x' variable
int x = new InnerClassConcepts().new InnerA().new InnerB().x;
InnerClassConcepts in = new InnerClassConcepts();
InnerA InnerA = in.new InnerA();
InnerA.InnerB xx = in.new InnerA().new InnerB();
System.out.println("InnerClassConcepts`s x = " + in.x);
System.out.println("InnerB`s x = " + xx.x);
System.out.println("main`s x(local variable) = " + x);
// System.out.println("InnerA`s x = " + InnerA.x);
System.out.println("InnerA`s y = " + InnerA.y);
}
/**
* Local inner class.
*/
class InnerA {
int y = 10;
/*
* java.lang.StackOverflowError coming over here... I don`t
* understand why?
*/
// int x=new InnerClassConcepts().new InnerA().new InnerB().x;//Line-29
/**
* Inner class inside an another Inner class.
*/
class InnerB {
private int x = 22;
int z = InnerA.this.y;
}
}
}
Output_
InnerClassConcepts's x = 11
InnerB's x = 22
main's x(local variable) = 22
InnerA's y = 10
我想知道为什么当我取消注释line-29StackOverflowError
时会出现。在方法上也是如此。line-29
main
有人可以帮助我哪里错了或者这背后的概念是什么?