简体   繁体   English

为什么包含main的类不必公开?

[英]Why doesn't the class containing main have to be public?

I declared the following class 我宣布了以下课程

class A { //not public
  public static void main(String args[]) {
     System.out.println("done");
 }

When I compile and run it, it runs fine and prints the output "done". 当我编译并运行它时,它可以正常运行并打印输出“ done”。 Same behavior even when I declare it as being in a "package a;" 即使我声明它在“ package a”中,也具有相同的行为;

However, if JVM spec mandates that main method should be public since "it can't see main otherwise", shouldn't it apply to the class as well? 但是,如果JVM规范要求主方法应该是公开的,因为“否则它就看不到主方法”,那么它也不适用于该类吗? If the JVM "can't see" A.main() when it is not declared public, how is it able to see the class A itself. 如果JVM在未声明为public时“看不到” A.main(),它将如何看到A类本身。

Is there any explanation for this other than "because the specification says so"? 除了“因为规范这样说”以外,对此还有什么解释吗?

The JVM has access to every class in the application all the time because one of its responsibilities is enforcing visibility rules. JVM始终可以访问应用程序中的每个类,因为它的职责之一是强制执行可见性规则。 Therefore, one can draw the conclusion that it can ignore visibility rules if need be (eg when the user starts the application, the JVM has to find the entry point, which is main() ). 因此,可以得出一个结论,即如果需要可以忽略可见性规则(例如,当用户启动应用程序时,JVM必须找到入口点,即main() )。

In other words, the JVM is not a class accessing this function, so visibility doesn't apply. 换句话说,JVM不是访问此函数的类,因此可见性不适用。 It is basically the overseer, managing the application from execution to termination. 它基本上是监督者,负责从执行到终止的整个过程。

For reference, see Execution . 有关参考,请参见执行

When you declare a class private , you're not making it "invisible", and the same goes for your methods. 当您将类声明为private ,您并不是使它“不可见”,方法也是如此。 Declaring a method private simply means it's not callable from outside your class. 声明一个private方法只是意味着它不能从您的类外部调用 A static public method of a private class is publicly callable. 私有类的静态公共方法是可公开调用的。

The reason the JVM can see a non-public class is because it controls visibility, meaning it sees everything and decides what can see/call/access what. JVM之所以可以看到非公共类,是因为它控制可见性,这意味着它可以看到一切并决定可以看到/调用/访问的内容。

The use of public on a class is different than on a method, but the concept is the same. 在类上使用public不同于在方法上使用public ,但是概念相同。

On a method, the public keyword means the method can be used outside the class. 在方法上, public关键字表示该方法可以在类之外使用。 An example would be: 一个例子是:

class A {
  public static void do() {
    // Do something
  }
}

class B {
  public static void main(String[] args) {
    A.do(); // This works because do() is public and static
  }
}

The same concept applies to classes, but in a different way. 相同的概念适用于类,但方式不同。

Using public on a class means that it can be used outside the current .java file (it will have its own .class file). 在类上使用public意味着可以在当前.java文件之外使用它(它将拥有自己的.class文件)。

Here's an example: 这是一个例子:

//C.java

  class C {
    static void do() {
      // Do something
    }

    public static void run() {
      A.do();  // Works because A.do() is public and static
      B.do();  // Does not work because B is not a public class
    }
  }



//A.java

  public class A {
    public static void main(String[] args) {
      B.do(); // Works because B is in the same file
      do();   // Duh...
    }

    public static void do() {
      // Do something
    }
  }

  class B {
    static void do() {
      // Do something
    }
  }

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

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