Access Modifiers In Java
Last modified on April 1st, 2014 by Joe.
Access modifiers specifies who can access them. There are four access modifiers used in java. They are public, private, protected, no modifer (declaring without an access modifer). Using ‘no modifier’ is also sometimes referred as ‘package-private’ or ‘default’ or ‘friendly’ access. Usage of these access modifiers is restricted to two levels. The two levels are class level access modifiers and member level access modifiers.
I) Class level access modifiers (java classes only)
Only two access modifiers is allowed, public and no modifier
- If a class is ‘public’, then it CAN be accessed from ANYWHERE.
- If a class has ‘no modifer’, then it CAN ONLY be accessed from ‘same package’.
II) Member level access modifiers (java variables and java methods)
All the four public, private, protected and no modifer is allowed.
- public and no modifier – the same way as used in class level.
- private – members CAN ONLY access.
- protected – CAN be accessed from ‘same package’ and a subclass existing in any package can access.
For better understanding, member level access is formulated as a table:
Access Modifiers
|
Same Class |
Same Package |
Subclass |
Other packages |
public |
Y |
Y |
Y |
Y |
protected |
Y |
Y |
Y |
N |
no access modifier |
Y |
Y |
N |
N |
private |
Y |
N |
N |
N |
First row {public Y Y Y Y} should be interpreted as:
- Y – A member declared with ‘public’ access modifier CAN be accessed by the members of the ‘same class’.
- Y – A member declared with ‘public’ access modifier CAN be accessed by the members of the ‘same package’.
- Y – A member declared with ‘public’ access modifier CAN be accessed by the members of the ‘subclass’.
- Y – A member declared as ‘public’ CAN be accessed from ‘Other packages’.
Second row {protected Y Y Y N} should be interpreted as:
- Y – A member declared with ‘protected’ access modifier CAN be accessed by the members of the ‘same class’.
- Y – A member declared with ‘protected’ access modifier CAN be accessed by the members of the ‘same package’.
- Y – A member declared with ‘protected’ access modifier CAN be accessed by the members of the ‘subclass’.
- N – A member declared with ‘protected’ access modifier CANNOT be accessed by the members of the ‘Other package’.
similarly interpret the access modifiers table for the third (no access modifier) and fourth (private access modifier) records.
Thanks for the explanation on java access modifiers / specifiers.
you are really doing a nice job for java. but all i want to say is that you should make your java program more user friendly for the java beginners to comprehend.
Thanks Milicent. I am writing for java beginners and intermediate level users. So what you say makes sense. I have received quite a lot of similar comments from java beginners. In recent java posts, I had this in mind and presenting simple java programs only. Will update some of the complex old java posts soon.
A class with no access modifier, in your terms, is “friendly or default”.
The way we (Java circles) like to define it is, “package-private”.
So, if a new Java developer learns to associate an empty access modifier
to “package-private” they’ll know its true visibility – it’s more explicit in
what it means. “Friendly” or “default” doesn’t describe what the visibility is.
“Friendly” will only make sense if the developer is coming from C++.
MJT, I accept your point on java access modifiers. Thanks, I have updated the post.
Hey regarding java access modifiers,
Suppose: I don’t care whether my java class as public, private or protected. So if I keep my class public or private or protected or default, based on the access modifiers I use, will it affect on performance of my java code?
Java access modifiers are about encapsulation dude not abt performance. Performance matter where it takes more resources to build or how other java class/thread interact with this class object and java access modifiers got nothing to do with it.
Thanks for the points on access modifiers…nice fundamental article
nice work…….
now i am good on access modifiers. can you write more on object oriented programming with java?
I need to download your java notes, please help..
should it be called java access modifiers or java access specifiers?
good work with the table, just what i need
nice job for beginner thanks………………..
Good job man… keep it up…
no access modifier – Subclasses in other package wont allow.
But in protected will can be accessed subclasses in another pacakge.
Hi Joe,
I think there are only three java access modifiers viz. “public”, “private” and “protected”. However, there are FOUR LEVELS of ACCESS viz. “public”, “private” “protected” and “default”.
its right answer i think
EXCELLENT THE WAY UR TEACHING
Thanks a lot for the explainig points on Java access modifiers…
are very very nice fundamental article and easy
tanx guyz. i was in doubt abt friendly. no modifier=friendly na? tanx.
what is the exact difference between Java Access Specifiers and Java Access Modifiers?
what is the type modifiers in Java?
Its very nice explanation..thnak you so much
@Vinayak: The java specification doesn’t classify and name the four types of access.
Therefore we all get to name it as the way we want. So for these conventions, I prefer to follow the majority.