Question: 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();
    }
}

```

Answer: 
```
NestedClass a = new NestedClass();

```

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

```
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);
}
class InnerClass{
    private String mysample = "Inner Class"; //InnerClass instance variable,
    NestedClass a = NestedClass.this;
    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 = nestedClass.innerClass; 
     nestedClass.getOuterS();
     nestedClass.getInnerS();
     nestedInner.getIn();
     nestedInner.getOut();
}

}

```
With this solution member class is `static`. For better comparison you might read 
[Static class declarations](http://www.javaworld.com/javaqa/1999-08/01-qa-static2.html) 

*Static nested classes (description)*

Static nested classes do not have access to non-static fields and methods of the outer class, which in some ways similar to the static methods defined within the class. Access to non-static fields and methods can only be done through an instance reference of the outer class. In this regard, static nested classes are very similar to any other top-level classes.

In addition, static nested classes have access to any static methods of the outer class, including to private.

The benefits of these classes is mainly in logical groupings of entities to improve encapsulation, as well as saving class-space.

```
public class NestedClass{
  private static String outer = "Outer Class"; //NestedClass instance variable
  InnerClass innerClass = new InnerClass();

  void getOuterS(){
    System.out.println(outer);
  }
  void getInnerS(){
    System.out.println(InnerClass.mysample);
  }
  InnerClass getInner(){
    return innerClass;
  }
  static class InnerClass{
    private static String mysample = "Inner Class"; //InnerClass instance variable,
     void getIn(){
      System.out.println(mysample);
    }
    void getOut(){
      System.out.println(outer); //access a static member
    }
  }
  public static void main(String[] args){
    NestedClass nestedClass = new NestedClass();
    NestedClass.InnerClass nestedInner = nestedClass.getInner();
    nestedClass.getOuterS();
    nestedClass.getInnerS();
    nestedInner.getIn();
    nestedInner.getOut();
  }
}

```