I have code like this I need to access the mysample variable of static class InnerClass in the getInnerS() method which is inside the the NestedClass. I tried accessing it by creating a new object for InnerClass but i am getting java.lang.StackOverflowError.
public class NestedClass{
private String outer = "Outer Class"; //NestedClass instance variable
NestedClass.InnerClass innerClass = new NestedClass.InnerClass();
void getOuterS(){
System.out.println(outer);
}
void getInnerS(){
System.out.println(innerClass.mysample);
}
static class InnerClass{
private String mysample = "Inner Class"; //InnerClass instance variable,
NestedClass a = new NestedClass();
void getIn(){
System.out.println(mysample);
}
void getOut(){
System.out.println(a.outer);
}
}
public static void main(String[] args){
NestedClass nestedClass = new NestedClass();
NestedClass.InnerClass nestedInner = new NestedClass.InnerClass();
nestedClass.getOuterS();
nestedClass.getInnerS();
nestedInner.getIn();
nestedInner.getOut();
}
}
in static InnerClass class creates an instance of the NestedClass and as InnerClass is static this is a loop.
InnerClass does not need to be static, this should work