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

前端 未结 10 2093
情书的邮戳
情书的邮戳 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 12:57

    A top-level class as private would be completely useless because nothing would have access to it.

    0 讨论(0)
  • 2020-12-07 12:57

    This needs a simple understanding of why it is not required to declare classes as private in Java.

    If your class is itself a standalone program/software (which is highly unlikely) then you would have already defined it in a project and a package specific for it. So adding the private keyword is redundant to it.

    If that's not the case then default access is then it means your program/software depends on different classes to run. If we are declaring one class among them as private (in case if we could) then we are restricting its accessibility by other classes, etc. which isn't of any use. It simply means that the class declared as private by us isn't of any use for the code to run. Which again renders it useless.

    If you mean package level access then for default access we don't need to declare any keyword before it.

    0 讨论(0)
  • 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).

    0 讨论(0)
  • 2020-12-07 13:05

    In theory, you could instantiate and call methods on a private top-level class (if such a thing were allowed by the language ... which it isn't!), but you would have to use reflection to do this. Sensibly (IMO) Sun decided that private top-level classes were not a good thing to support at the language level.

    Actually, it is possible that the JVM might support top-level private "classes" created by bytecode magic. But it is not a useful thing to do.


    UPDATE - In fact, the current JVM spec makes it clear that the ACC_PRIVATE bit of the access flags word for a class is "reserved for future use", and that Java implementations should treat it as unset. Thus, the above speculation is moot for any JVM that strictly implements the current specification.

    0 讨论(0)
  • 2020-12-07 13:05

    There would be no way to access that class or its members.

    0 讨论(0)
  • 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".

    0 讨论(0)
提交回复
热议问题