I'm new to Java, learning Java from the Oracle Java tutorial. I'm now learning about nested classes, static classes and inner classes. I found the following explanation which seems odd, and I think it is wrong.
From: https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html
A nested class is a member of its enclosing class. Non-static nested classes (inner classes) have access to other members of the enclosing class, even if they are declared private. Static nested classes do not have access to other members of the enclosing class
The last sentence "Static nested classes do not have access to other members of the enclosing class" is strange, but may refer to instance members, saying the static class is like a static method, having no access to instance variables. But the next note is even stranger:
Note: A static nested class interacts with the instance members of its outer class (and other classes) just like any other top-level class. In effect, a static nested class is behaviorally a top-level class that has been nested in another top-level class for packaging convenience.
This seems odd, as it implies that a static class cannot access private instance members of the outer class. I've written the following code which compiles and runs, and demonstrates that a static class can access outer instance private variables.
public class A {
private int x;
static private int y;
static public class B{
static void doSomething(){
y++;
System.out.println("y is now " + y );
}
static void doSomethingElse(A a)
{
a.x++;
System.out.println("a.x is " + a.x );
}
}
}
// ------
public class Main {
public static void main(String[] args){
A a = new A();
A.B b = new A.B();
b.doSomething();
b.doSomethingElse(a);
}
}
Is this a mistake in the tutorial, or am I maybe not understanding something well? Thanks