0

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

Cedric
  • 25
  • 5

2 Answers2

0

My solution is this... If you want do it you must put all references of that static nested class with static property. Read also something of this Java inner class and static nested class

public class MyClass {
    public static void main(String args[]) {
       method1();
       System.out.println("main:   " + NestedClass.data1);
    }
    public static void method1(){
        System.out.println("method1:  "+ NestedClass.data1);
    }
    static class NestedClass{
        static String data1 = "Something";
    }
}
dherediat
  • 146
  • 13
  • Thank you! However, I am hung up on the apparent inconsistency with the syntax; you can access a static member of a class1 from class2 through a reference to an object of class1...unless that static member is a nested class??? – Cedric Jan 12 '18 at 19:34
  • i understood other thing sorry – dherediat Jan 12 '18 at 20:01
0

innerClass is just like any Class and is not a static member of any class including outerClass. The only difference with innerClass is that you reference it from outside outerClass using fully qualified name outerClass.innerClass.

This works: outerClass.innerClass.i (BTW use proper Java class naming to avoid confusion with variable names).

tsolakp
  • 5,858
  • 1
  • 22
  • 28
  • Thanks so much! First, please elaborate on how the nested class isn't a member..."A nested class that is declared directly within its enclosing class scope is a member of its enclosing class" (Java, The Complete Reference, 9th Ed.)... I'm not arguing with you, just really want to figure this out because its really confusing me. Also, what is proper Java class naming? – Cedric Jan 12 '18 at 19:45
  • It is not a member in the sense `j` is member of `outerClass`. – tsolakp Jan 12 '18 at 19:50
  • Is it a member only in the sense of being within the enclosing scope of `outerClass`, and in no other sense? The syntax of outerClass.innerClass suggests it's an actual member. Is this syntax misleading therefore? Please inform. – Cedric Jan 12 '18 at 19:56
  • What is the purpose of the static modifier if it is not a member? This subject is very confusing to me and is very poorly explained in the text I'm using. – Cedric Jan 12 '18 at 19:58
  • There is difference between nested class that is declared as static and the one that is not (also called inner class). See how they are different: https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html – tsolakp Jan 12 '18 at 20:00