Java – internal static class field visibility
•
Java
I have two classes, as follows
public class A{
private static class B{
private static int s1;
private static int s2;
private int x;
}
}
In class B, all fields are private and should not be accessed in class A But there is a 'mystery', static fields can be in class A and class B
private int x
Not accessible My question is why private static fields can be accessed in class a?
Note: my class B is static and its constructor is private
Solution
In addition to the answer given by @ UWE plonus (1), I can assume that you try to access the non static field X through class B without creating an instance, that is
B.x = 5;
You must create an instance of Claas B to access its instance members, i.e
new B(). x = 5;
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
