Possible Duplicate:
Java: Why can we define a top level class as private?
Why can't we declare a private outer class? If we can have inner private class then why can't we have outer private class...?
It is only applicable to constructor, methods, and fields inside the classes. If a variable or methods or constructor is declared as private then we can access them only from within the class i.e from outside the class we can't access them.
No, we cannot declare a top-level class as private or protected. It can be either public or default (no modifier).
A top-level class as private would be completely useless because nothing would have access to it. If a top level class is declared as private the compiler will complain that the "modifier private is not allowed here" . This means that a top level class cannot be private.
Unlike a class, an inner class can be private and once you declare an inner class private, it cannot be accessed from an object outside the class. Following is the program to create an inner class and access it. In the given example, we make the inner class private and access the class through a method.
Private outer class would be useless as nothing can access it.
See more details:
Java: Why can we define a top level class as private?
private
modifier will make your class inaccessible from outside, so there wouldn't be any advantage of this and I think that is why it is illegal and only public
, abstract
& final
are permitted.
Note : Even you can not make it protected
.
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