0

I'm learning about nested classes. I just want to know why I'm not able to access a static variable of the outer class from a static inner class using an instance of it.

class MyListner {
    static String name = "StaticApple";
    String nonStaticName = "NonStaticApple";

    static class InnerMyListner {
        void display(){
            System.out.println("Static variable of outer class: " + name);
        }
    }

    private static final MyListner singleton = new MyListner();

    private MyListner() {

    };

    public static MyListner getInstance() {
        return singleton;
    }
}

public class Results{
    public static void main(String[] args) {
        MyListner.getInstance();

        MyListner.InnerMyListner innerclassO = new MyListner.InnerMyListner();
        innerclassO.display();  // This works
        String staticVariable = innerclassO.name;  // Why doesn't this work?
    }
}
6
  • 1
    name isn’t a property of InnerMyListener, it’s a property of MyListener Commented Feb 10, 2019 at 13:51
  • At least because InnerMyListner does not have field named name Commented Feb 10, 2019 at 13:52
  • You need this: String staticVariable = MyListner.name; // Why doesn't this work? Commented Feb 10, 2019 at 13:53
  • @MTCoster, Yes name is not a property of InnerMyListener, but Static nested class can access Static members of enclosing class..Nope ? Then how am able to access using display() method ...? Commented Feb 10, 2019 at 13:56
  • @CookingJava see my answer. Commented Feb 10, 2019 at 14:01

1 Answer 1

1

You have to understand how class(es) works here. InnerMyListner class is an static nested class.

As with class methods and variables, a static nested class is associated with its outer class.

While the static nested class cannot access the outer class' instance properties, it can access static properties (shared by all the instances), which are inside the visibility scope.

Inside Results you're out of the visibility scope for name.
For a more in depth overview, see Java documentation

Sign up to request clarification or add additional context in comments.

2 Comments

I'm calling static variable for InnerMyListner class's usage. I can do it using Inner class's method but why not without them ..you said in words(As with class methods and variables, a static nested class is associated with its outer class.) ..but am asking why its not possible ? Accessing with a method or without ..shouldn't make any difference ...because am not breaking the law here which says static inner class can only access to static members of outer class...!
@CookingJava just look at where the "name" property access is. One is inside the containing class "MyListner" (InnerMyListner#display), the other is totally outside its context. Think about it and it will be evident why this happens. You can use the "display" method because it the static class methods are visibile to the outsite.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.