Oracle Java tutorial – static classes – possible error in tutorial

  • A+

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.


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


Is this a mistake at the tutorial, or maybe I'm not understanding somwthing well?

The error is in your understanding, and the tutorials are correct. Nowhere within your nested static class is there any direct manipulation of the instance fields of the outer class. I'm talking about these fields without an instance attached -- nowhere can you directly manipulate x without having it attached to an A instance.

So you can do this:

static void doSomethingElse(A a) {     a.x++;  // x is part of the A instance passed into a parameter     System.out.println("a.x is " + a.x ); } 

but you can't do this:

static void doSomethingElse2() {     x++;     System.out.println("x is " + x ); } 

And this code would be the same if B were static nested or a stand-alone non-nested class.

You ask:

"A static nested class interacts with the instance members of its outer class just like any other top-level class"?

Exactly as is shown above -- a non-static nested class can directly interact with the a field (as doSomethingElse2() shows) without need of a supporting A instance, while both a static nested class and a stand alone class cannot. They both require the separate A instance, here which is passed into your doSomethingElse(A a) method parameter.

The main difference between a static nested and a stand-alone is that the former, the nested class, has access to private members of the outer class while the stand-alone does not. Perhaps this is your source of confusion.


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: