0

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();
    }
}
Roman C
  • 49,761
  • 33
  • 66
  • 176
Prakash_se7en
  • 88
  • 2
  • 3
  • 16

3 Answers3

3

In InnerClass constructor:

NestedClass a = new NestedClass(); 

So, you create a new NestedClass, which creates a new InnerClass, which creates itself its own NestedClass, with the corresponding InnerClass.... No wonder the stackoverflow.

If you want to access the enclosing class, you should use (inside InnerClass methods)

NestedClass.this   
SJuan76
  • 24,532
  • 6
  • 47
  • 87
  • i changed and deleted NestedClass a = new NestedClass(); and changed as System.out.println(NestedClass.this.outer); in the inner class but i am getting compilation error saying "No enclosing instance of the type NestedClass is accessible in scope" – Prakash_se7en Sep 08 '12 at 15:34
1
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();
}


}
Nahuel Fouilleul
  • 18,726
  • 2
  • 31
  • 36
  • Thank you very much .....Nahuel Fouilleul i gotit..if you dont mind can you say why are we using this there..?? – Prakash_se7en Sep 08 '12 at 15:39
  • +1 for removing static allowing `NestedClass.this` to be used – Reimeus Sep 08 '12 at 15:43
  • if InnerClass is static it cannot access member of outer class that are not static, but if it's not there is at least one instance of outer class which can be acceced, the one which created the nested instance. Sorry if it's not clear maybe someone else can explain better – Nahuel Fouilleul Sep 08 '12 at 15:45
  • A static inner class is stritly identical to an ordinay outer level class with the exception that its name is hidden under the parent class. It's only purpose is to permit the quick creation of classes without having to create a new file for each of them. Without the word static, these classes are quite different: upon their creation (ie., when we create an object from these subclasses), the address of their parent object (the value of "this" for the parent) is passed as a hidden parameter to each of them; therefore given them the possibility of accessing the non-static members of the parent. – SylvainL Sep 08 '12 at 16:40
0

With this solution member class is static. For better comparison you might read Static class declarations

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();
  }
}
Roman C
  • 49,761
  • 33
  • 66
  • 176