When a nested class instance is created, how does it refer to the outer class? Does it always propagate the outer class or refer to it in another way? I was told that the internal extension is external, but then why the following example does not work?
Example:
public class OuterClass { public String fruit = "apple"; public class InnerClass { public String fruit = "banana"; public void printFruitName(){ System.out.println(this.fruit); System.out.println(super.fruit); } } }
The above does not compile with an error for super.fruit saying that the "fruit" cannot be resolved. However, if the inner class is specified to extend the outer class, then it works:
public class OuterClass { public String fruit = "apple"; public class InnerClass extends OuterClass { public String fruit = "banana"; public void printFruitName(){ System.out.println(this.fruit); System.out.println(super.fruit); } } }
This seems to indicate that the inner class does not extend the outer class unless specifically indicated.
java reference inner-classes nested nested-class
Continuity8
source share