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

前端 未结 4 1319
春和景丽
春和景丽 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条回答
  •  囚心锁ツ
    2020-12-17 06:47

    Probably the designers of JLS decided there is no need to restrict access to main method if you know the class name, while at the first glance it looks counter-intuitive; from the other side - the access could be always got via reflection so it couldn't be treated as a security hole... Anyway e.g. by creating facade to a package-private class we access that one indirectly... So the protection is rather from incorrect usage and to allow further changes.

提交回复
热议问题