Package-private class within a .java file - why is it accessible?

前端 未结 4 1405
春和景丽
春和景丽 2020-12-17 06:19

Consider the following code, where the HelloWorld class has default or package-private access:

class HelloWorld {
    public static void main(St         


        
4条回答
  •  -上瘾入骨i
    2020-12-17 06:31

    JVM startup is described in §12.1 Virtual Machine Start-Up of the JLS.

    Note that this chapter says nothing about visibility checks with regards to the class. It only specifies that the main method must be public.

    This means that there simply is no check for visibility on the class level (which kind-of makes sense as there is no context yet against which to check the visibility: in which "package" is the "caller"?).

提交回复
热议问题