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"?
No, we cannot declare a top-level class as private or protected. It can be either public or default (no modifier).
As a member of the OuterClass , a nested class can be declared private , public , protected , or package private. (Recall that outer classes can only be declared public or package private.)
Explanation: If we make any class constructor private, we cannot create the instance of that class from outside the class.
A top-level class as private would be completely useless because nothing would have access to it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With