Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Access Modifier for Class

 
edirg559
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1847
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
edirg559
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic