Home Accessing static nested classes through references to outer class members
Reply: 0

Accessing static nested classes through references to outer class members

user785
1#
user785 Published in May 20, 2018, 10:00 am

I'm a beginner struggling with nested classes in general. Here is the first of what will likely amount to several questions on this subject.

In Java, why can't a static nested class be accessed through a reference to an object of the outer class, like other static members.

For example,

class program {
     public static void main(String[] args) {
          outerClass outerClassRefVar = new outerClass();
          int k;
          //k = outerClassRefVar.innerClass.i; doesn't compile
          k = outerClassRefVar.j; //does compile
     }
}

class outerClass {
     static class innerClass {
          static int i = 1;
     }
     static int j = 1;
}

...but innerClass and j are both static members of outerClass

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.247007 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO