简体   繁体   English

为什么我可以从 main 方法访问私有变量?

[英]Why can I access a private variable from main method?

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 ?我有一个简单的问题.... 为什么我可以从 main 方法访问一个私有变量? 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?我相信 main 不是包含它的类的一部分......然后我不会联系到一个私人成员,但我可以......为什么? Please help...thx请帮助...thx

Classes can access the private instance variables of (other) objects of the same type.类可以访问相同类型(其他)对象的私有实例变量。

The following is also possible以下也可以

public class Foo {

    private int a;

    public void mutateOtherInstance(Foo otherFoo) {
        otherFoo.a = 1;
    }
}

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.您可能会争论这是否合乎需要,但这只是 JLS 指定这是合法的生活规则。

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. Main 是你的类的一部分,你已经在你的类中声明了它 :) main 不是你的对象的一部分,它不会是你从类创建的对象的任何部分,但它仍然是类的一部分。 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 只是一个普通的静态函数,框架知道它应该在程序执行时查找。

main方法在类Ferrari ,因此可以访问私有变量,即使它是静态的。

Well, main() is part of the containing class.好吧, main()包含类的一部分。 In fact, main() is exactly like every other method, except that you can start the JVM and tell it to run the main() method of a class from the command line.实际上, main()与其他所有方法完全一样,只是您可以启动 JVM 并告诉它从命令行运行类的main()方法。

As long as the private variable is in the same class as the main() method, then the main() method has access to it.只要私有变量与 main() 方法在同一个类中,那么 main() 方法就可以访问它。 In general, even static methods have access to private fields of instances of the same class.通常,即使是静态方法也可以访问同一类实例的私有字段。

The only special feature of the main method is it is used to tell the compiler where program execution should begin. main 方法唯一的特点是它用于告诉编译器程序执行应该从哪里开始。 Other than that it behaves just like any other class method and has access to private variables like any other class method.除此之外,它的行为就像任何其他类方法一样,并且可以像任何其他类方法一样访问私有变量。

Because main is static and your class hasn't been instantiated.因为 main 是静态的,并且您的类尚未实例化。

eg, you have no Ferrari object to access.例如,您没有要访问的法拉利对象。 You must create a Ferrari object then access it's members.您必须创建一个 Ferrari 对象,然后访问它的成员。 static main is a special static function. static main 是一个特殊的静态函数。 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.所以如果你将你的主要方法移到法拉利之外,你会期望你必须创建一个法拉利的实例来使用它......这里也是一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM