This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Access Modifier for Class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Access Modifier for Class" Watch "Access Modifier for Class" New topic
Author

Access Modifier for Class

edirg559
Greenhorn

Joined: Apr 19, 2006
Posts: 19
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?

Thanks,
Erik
Jeroen T Wenting
Ranch Hand

Joined: Apr 21, 2006
Posts: 1847
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.


42
edirg559
Greenhorn

Joined: Apr 19, 2006
Posts: 19
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?

Thanks,
Erik
Shyam Prasad Murarka
Ranch Hand

Joined: May 02, 2005
Posts: 209
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 class BUT 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.


With Best Regards,
Shyam Prasad Murarka
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Access Modifier for Class
 
Similar Threads
class access modifier
Private Access Modifier for class
Doubt in Mocktest
interface error
Top level Class can be private,protected?