@ Nikita Rybak's answer has good points, but lack of details, I can’t just get an idea without thinking deeply about myself, the following is what I thought, and now I have to fully understand the reason.
Four access modifiers, suppose 1st level is public and 4th level is private (based on this table in sequence). The first thing we should know is why a class cannot be defined as closed at the top level.
So if "private class foo" (a specific private member, i.e. the class itself is a member) allows what is external (which contains the member)? File size? No, an external file does not make sense, because even several classes in one file will be compiled into separate class files. So the external is the packaging . But the default access modifier of level 3 already means "private package ". Thus, the level 4 private access modifier will not be used / allowed.
But a nested private class is allowed because the direct external is the class, not the package, for example :
class PrivateNestedMain { private static class Inner { public static void main(String[] args) { System.out.println("Hello from Inner!"); } } }
But what if the "protected class foo" allows? The protected main characteristic is the subclass, so the external (package) MUST (due to volume, but still not necessary) provide the style of the subclass , i.e. the subpackage, or package A extends package B , but we don’t know anything like that. Thus, the protected cannot use the full potential (the main scope of the subclass) at the top level, which is the package outside (i.e., such a subpacket does not exist), but the protected can use the full potential in the nested class, which is the class external (t .e. may be a subclass) :
class ProtectedNestedMain { protected static class Inner { public static void main(String[] args) { System.out.println("Hello from Inner!"); } } }
Please note that the above “cannot use the full potential” because it cannot reach the whole subclass just because the external subclass cannot be resolved , that is, it is actually protected , it’s just a matter of choice, to avoid duplication of the package’s work-private. if external is not subclass , see below.
My confusion is mainly caused by a famous table at https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html :

If the 1st level (public) and the 3rd level (private package) are allowed, then how, in fact, the intermediate level of the 2nd (protected) is not allowed?
Public support for a subclass is so easy to mislead. The correct way to read this table
public helper subclass if the outer has a subclass property.
The same misleading application to a private package, a private package does not support a subclass ( N in a cell) does not mean that the concept of a subclass is applied externally.
This means that we should ignore the Subclass column if the subclass function is not available in external:

As we see now, both the protected and closed packages are at the same level ( YYN ), there is no more confusion as to why the intermediate layer is not allowed. In general, Java selects only a closed package, not a protected one, to avoid confusion ( this is just a matter of choice , but the main characteristic of a protected object is a subclass, so the package’s privacy is higher), and as a result , only 2 access modifiers are allowed at the top level:
At the top level, public or package-private (without an explicit modifier).