I am working on Inner Class concepts and write the below code_
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
I am wondering why StackOverflowError
is coming on line-29
when i uncomment line-29. Where as the same is working in main
method.
Can somebody please help me where i am wrong or what concept is behind this?