为什么我可以从 main 方法访问私有变量?
package com.valami;
public class Ferrari
{
private int v = 0;
private void alam()
{
System.out.println("alam");
}
public Ferrari()
{
System.out.println(v);
}
public static void main(String[] args)
{
Ferrari f = new Ferrari();
f.v = 5;
System.out.println(f.v);
}
}
大家好! 我有一个简单的问题......为什么我可以从 main 方法访问私有变量?我知道,我在包含类中,但它是主要的。我相信 main 不是包含它的类的一部分...那么我不会到达私人成员,但我可以...为什么? 请帮忙...谢谢
package com.valami;
public class Ferrari
{
private int v = 0;
private void alam()
{
System.out.println("alam");
}
public Ferrari()
{
System.out.println(v);
}
public static void main(String[] args)
{
Ferrari f = new Ferrari();
f.v = 5;
System.out.println(f.v);
}
}
Hi all!
I have one simple question.... WHY can I reach a private variable from the main method ? I know, I'm in the containing class, but it is main. I believed the main is NOT part of the class which is containing it... Then I would not to reach an private member, but I can....WHY?
Please help...thx
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
类可以访问相同类型的(其他)对象的私有实例变量。
以下情况也是可能的。
您可以争论这是否合乎需要,但这只是 JLS 指定这是合法的生活规则。
Classes can access the private instance variables of (other) objects of the same type.
The following is also possible
You could argue if this is desirably or not, but it's just a rule of life that the JLS has specified this is legal.
Main 是你班级的一部分,你已经在你的班级中声明了它:)
main 不是您的对象的一部分,它不会是您从类创建的对象的任何部分,但它仍然是类的一部分。这对于任何静态函数都是正确的,因为 main 只是一个普通的静态函数,框架知道它应该在执行程序时查找。
Main is a part of you class, you have declared it inside your class :)
What main is not is part of your object, it will not be any part of the objects you create from the class but it is still part of the class. This is correct for any static function as main is just a normal static function that the framework knows it should look for when the program is executed.
main
方法位于类Ferrari
中,因此可以访问私有变量,即使它是静态的。The
main
method is in the classFerrari
and thus can access private variables, even if it's static.嗯,
main()
是包含类的一部分。事实上,main()
与所有其他方法完全相同,只是您可以启动 JVM 并告诉它从命令运行类的main()
方法线。Well,
main()
is part of the containing class. In fact,main()
is exactly like every other method, except that you can start the JVM and tell it to run themain()
method of a class from the command line.只要私有变量与 main() 方法位于同一类中,那么 main() 方法就可以访问它。一般来说,即使是静态方法也可以访问同一类实例的私有字段。
As long as the private variable is in the same class as the main() method, then the main() method has access to it. In general, even static methods have access to private fields of instances of the same class.
main 方法的唯一特点是它用于告诉编译器程序应该从哪里开始执行。除此之外,它的行为就像任何其他类方法一样,并且可以像任何其他类方法一样访问私有变量。
The only special feature of the main method is it is used to tell the compiler where program execution should begin. Other than that it behaves just like any other class method and has access to private variables like any other class method.
由于您已将 main 方法与私有属性 v 放在同一个类中,因此可以访问它。私有属性或方法可由其自己的类访问。
也就是说,如果将 main 方法移至另一个类,则无法访问具有 private 修饰符的任何内容。
Since you have made the main method in the same class as the private attribute v, you can access it. Privates attribute or methods are accessible to its own class.
That is to say, if you moved the main method to another class, you cannot access anything that has a private modifier.
因为 main 是静态的,并且您的类尚未实例化。
例如,您没有可以访问的 Ferrari 对象。您必须创建一个 Ferrari 对象然后访问它的成员。 static main 是一个特殊的静态函数。如果您愿意,您可以将其视为独立的。因此,如果您将主要方法移到 Ferrari 之外,您会期望您必须创建 Ferrari 的实例才能使用它......这里也是如此。
Because main is static and your class hasn't been instantiated.
e.g., you have no Ferrari object to access. You must create a Ferrari object then access it's members. static main is a special static function. You can think of it as sort of separate if you want. So if you moved your main method outside of Ferrari you would expect that you would have to create an instance of Ferrari to use it... same deal here.