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?
}
}
nameisn’t a property ofInnerMyListener, it’s a property ofMyListenerInnerMyListnerdoes not have field namedname