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

前端 未结 3 1744
迷失自我
迷失自我 2020-12-18 10:31

I declared the following class

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

When

3条回答
  •  时光说笑
    2020-12-18 11:11

    The JVM has access to every class in the application all the time because one of its responsibilities is enforcing visibility rules. Therefore, one can draw the conclusion that it can ignore visibility rules if need be (e.g. when the user starts the application, the JVM has to find the entry point, which is main()).

    In other words, the JVM is not a class accessing this function, so visibility doesn't apply. It is basically the overseer, managing the application from execution to termination.

    For reference, see Execution.

提交回复
热议问题