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

前端 未结 10 2123
情书的邮戳
情书的邮戳 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:07

    I believe a better question would be:

    What would it mean for a top level class to be private?

    If you think in terms of access levels, the level above class is package. In fact you can have package private top level classes in Java! Taking from the Oracle (formerly Sun) Java tutorials:

    If a class has no modifier (the default, also known as package-private), it is visible only within its own package (packages are named groups of related classes — you will learn about them in a later lesson.)

    Depending on the answer to the question I asked, this might fit with your definition of a "top level private class".

提交回复
热议问题