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

Java

Java Problem Overview


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"?

Java Solutions


Solution 1 - Java

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

Solution 2 - Java

Java doesn’t allow a top level class to be private. Only 'public' or 'package'.

Solution 3 - Java

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".

Solution 4 - Java

Private classes are allowed, but only as inner or nested classes. If you have a private inner or nested class, then access is restricted to the scope of that outer class.

If you have a private class on its own as a top-level class, then you can't get access to it from anywhere. So it does not make sense to have a top level private class.

Solution 5 - Java

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.

Solution 6 - Java

We can not declare an outer class as private. More precisely, we can not use the private access specifier with an outer class. As soon as you try to use the private access specifier with a class you will get a message in Eclipse as the error that only public, final, and abstract can be used as an access modifier with a class.

Making a class private does not make any sense as we can not access the code of its class from the outside.

Solution 7 - Java

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

Solution 8 - Java

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.

Solution 9 - Java

As we already know, a field defined in a class using the private keyword can only be accessible within the same class and is not visible to the outside world.

So what will happen if we will define a class private? Will that class only be accessible within the entity in which it is defined which in our case is its package?

Let’s consider the below example of class A

package com.example;
class A {
    private int a = 10;

    // We can access a private field by creating object of same class inside the same class
    // But really nobody creates an object of a class inside the same class
    public void usePrivateField(){
        A objA =  new A();
        System.out.println(objA.a);
    }
}

Field ‘a’ is declared as private inside ‘A’ class and because of it, the ‘a’ field becomes private to class ‘A' and can only be accessed within ‘A’. Now let’s assume we are allowed to declare class ‘A’ as private, so in this case class ‘A’ will become private to package ‘com.example’ and will not be accessible from outside of the package.

So defining private access to the class will make it accessible inside the same package which the default keyword already does for us. Therefore there isn't any benefit of defining a class private; it will only make things ambiguous.

You can read more in my article Why an outer Java class can’t be private or protected.

Solution 10 - Java

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).

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSatyaView Question on Stackoverflow
Solution 1 - JavaMaximilian MayerlView Answer on Stackoverflow
Solution 2 - JavaGuillaumeView Answer on Stackoverflow
Solution 3 - JavastevebotView Answer on Stackoverflow
Solution 4 - JavaAniket ThakurView Answer on Stackoverflow
Solution 5 - JavaStephen CView Answer on Stackoverflow
Solution 6 - JavaRajnishView Answer on Stackoverflow
Solution 7 - JavaRangerView Answer on Stackoverflow
Solution 8 - JavaNikhil AroraView Answer on Stackoverflow
Solution 9 - JavaNaresh JoshiView Answer on Stackoverflow
Solution 10 - JavaTimothy PerezView Answer on Stackoverflow