Skip to main content

Static nested classes

1 reply [Last post]
moqiliang
Offline
Joined: 2009-01-30

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 Java Tutorials

Static nested classes do not have access to other members of the enclosing class?But the code below also works well:
class A {
private static int x;
public static class B {
int y = x;
}
}

"x" is a member of A in which the static class B define, the only difference is "x" is defined as static, but static member is also a member of it's class. I am very confused...
reference link:http://java.sun.com/docs/books/tutorial/java/javaOO/nested.html

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
fardoonmuhafiz
Offline
Joined: 2009-11-04

x is a static variable and the static nested class B is just like a static method, which has access to static fields but it doesnt have access to instance fields of the enclosed class A.

class A{
private static int x;
int z;
public static class B{
int y=x;//is fine
int a=z;//compiler says: cannot make a static reference to a non-static field z..
}
}///hope your confusion is cleared....