Why can't we define a top level class as private?

前端 未结 10 2114
情书的邮戳
情书的邮戳 2020-12-07 12:38

Why does Java not allow a top level class to be declared as private? Is there any other reason other than \"We can\'t access a private class\"?

10条回答
  •  时光取名叫无心
    2020-12-07 13:03

    You cannot define a top level class private (or anything else besides public). You will get a compilation error.

    Something.java:6: error: modifier private not allowed here
    private class Something {
            ^
    1 error
    

    You have only two options, public or no access modifier at all. By omitting public you, implicitly, limit class access to within the package (aka: package-private).

提交回复
热议问题