I always learned that we use public to declare a class. Is that mean we can not delcare a class using private access modifier?
At the same time I also learned that we may use private access modifier for a class only if it is an inner class. It is to strengthen the security issue. Is this something recommended on real-life practice?
A private upper level class would make no sense as nothing could ever instantiate it or access static methods on it.
Classes therefore are always public or have package level access.
Private inner classes are just like private data members, they can be accessed only from the class that defines them. This means you can't create an instance of one from another class by doing something like Outer.Inner instance = new Outer().new Inner();
By the rule that you should always make members as inaccessible as possible that often makes perfect sense.
Joined: Apr 19, 2006
Another related question again, what would be the default access modifier if I don't declare one for the class while declaring it. Is it going to be public one?
Dear Reader, If you do not explicitly specify any access modifier for a class then it is NOT given the scope similar to the public modifier. Instead it is given a scope such that anyone in the same package can access that classBUT outside the package no one can access it. So its scope is somewhat similar to the public modifier minus the ability to be seen outside the package. My teacher always called this scope as "friendly". I am not even sure if its a term. I think he just made it up or something. BUT I am definitely sure that it is NOT a keyword.
Originally posted by Shyam Prasad Murarka: ...My teacher always called this scope as "friendly". I am not even sure if its a term. I think he just made it up or something. BUT I am definitely sure that it is NOT a keyword.
There is no official term for the default access level. It is often called "package" (as Jeroen mentioned above), and sometimes called "friendly" (as Shyam mentioned), or less commonly "package friendly" or even "package private."
(If you're going to call it anything other than "default," I personally think "package" is the most descriptive.)
Note that there is no keyword for this access level. But be careful, because "default" is a keyword, as it's used in a switch/case statement.
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer sscce.org