File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Access modifiers for class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Access modifiers for class" Watch "Access modifiers for class" New topic

Access modifiers for class

Rajiv Rai
Ranch Hand

Joined: Jun 14, 2010
Posts: 57
I read in SCJP book that applying private and protected
access modifiers to a class doesn't make sense,,

Please help me to understand why it doesnt not make any sense to apply those
two modifiers to classes..

Jelle Klap

Joined: Mar 10, 2008
Posts: 1951

For top-level classes that's true, and nto only does it not make sense, the compiler will not even allow the use of those modifiers.
Have a look at this page on access modifiers, and try to figure out why that is.

Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Matthew Brown

Joined: Apr 06, 2010
Posts: 4543

Think about what it would mean if they were allowed.

For example, private. What would a private class mean? It would probably be a class that was not visible to any other class. So what use would it be? Can you think of a situation where you would want that?

This is often the best way to go about resolving any queries along the lines of "why isn't that modifier allowed here". Think about what the effect would be if it was allowed, and when you might need that. Usually it just wouldn't make sense.
Jelle Klap

Joined: Mar 10, 2008
Posts: 1951

Also, have a look at this topic.
I agree. Here's the link:
subject: Access modifiers for class
It's not a secret anymore!