[英]Java - private inner class public member
how does the access level modifier change the behavior of a member inside an inner private class? 访问级别修饰符如何改变内部私有类中成员的行为? I have this code
我有这个代码
class Main {
private static class Inner {
private static int a = 1;
}
public static void main(String[] args) {
System.out.print(Inner.a);
}
}
I can access the attribute "a" from the outer class either it's access level is public or private (and this is already weird for me). 我可以从外部类访问属性“ a”,无论它的访问级别是公共的还是私有的(这对我来说已经很奇怪了)。 The question is: if the attribute were " public static int ", what were the difference (considering that the Inner class is private so inaccessible from the extern of the "Main" class)?
问题是:如果该属性为“ public static int ”,则有什么区别 (考虑到Inner类是私有的,因此从“ Main”类的外部无法访问)?
To recap 回顾
I can see no difference 我看不出有什么不同
Inner
is private, it can be accessed only from within Main
. Inner
是私有的,只能从Main
内部访问。 Even if a
is public, but Inner
is private and cannot be accessed from a particular place, attribute of a
would be irrelevant. 即使
a
是公开的,但Inner
是私有的,不能从某一特定地点访问的属性a
是无关紧要的。
class Main {
private static class Inner {
public static int a = 1;
}
public static void main(String[] args) {
System.out.print(Inner.a);
}
}
When you run , 跑步时
1
Whenever Inner
is static in the same class,( Main
), Inner
(and all its members) are always accessible from anywhere in the Main
class. 只要
Inner
在同一个类中是静态的( Main
),就始终可以从Main
类中的任何位置访问Inner
(及其所有成员)。 It is private outside of Main
Class. 它是
Main
Class以外的私有对象。 If I were to access 如果我要访问
Main main = new Main();
main.Inner; // Compilation Failure. Inner is not accessible
// Technically I would never do new Main(), but to illustrate I am doing so.
Or a more sensible example of the same 还是一个更明智的例子
class Main {
public static void main(String[] args) {
System.out.print(SomeClass.Inner);
}
}
class SomeClass{
private static class Inner {
public static int a = 1;
}
}
To illustrate the difference of making a
private or public, consider the following two examples 为了说明做的差
a
私人或公共,考虑下面两个例子
class Main {
public static void main(String[] args) {
System.out.print(SomeClass.Inner.a);
}
}
class SomeClass{
public static class Inner {
private static int a = 1; // If a is private, it produces an error above
//public static int a = 1; // This would not be an error
}
}
So in your particular case, as long as it is in same class Main
, it is irrelevant, but if it is outside as next example, it would matter. 因此,在您的特定情况下,只要它在
Main
类中,就没有关系,但是如果在下一个示例中它在外部,则将很重要。
You can think of your Inner
class as a class-level attribute. 您可以将您的
Inner
类视为类级别的属性。 Making it private prevents access to it outside of the class. 将其设为私有可防止在类之外访问它。 Since it's an inner class, the inner class's variables belong to the outer class and are thus accessible to the outer class, regardless of the access modifiers.
由于它是内部类,因此内部类的变量属于外部类,因此,无论访问修饰符如何,外部类都可以访问它们。
http://docs.oracle.com/javase/tutorial/java/javaOO/innerclasses.html http://docs.oracle.com/javase/tutorial/java/javaOO/innerclasses.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.