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

II) Member level access modifiers (java variables and java methods)

All the four public, private, protected and no modifer is allowed.

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:


Second row {protected Y Y Y N} should be interpreted as:

similarly interpret the access modifiers table for the third (no access modifier) and fourth (private access modifier) records.

Comments on "Access Modifiers In Java"

  1. MILICENT ANYAEGBU says:

    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.

  2. Joe says:

    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.

  3. mjt says:

    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++.

  4. Joe says:

    MJT, I accept your point on java access modifiers. Thanks, I have updated the post.

  5. Priyank says:

    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?

  6. raj roy says:

    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.

  7. Satendra Dixit says:

    Thanks for the points on access modifiers…nice fundamental article

  8. VIRAT GANDHI says:

    nice work…….

  9. henok bekele says:

    now i am good on access modifiers. can you write more on object oriented programming with java?

  10. henok bekele says:

    I need to download your java notes, please help..

  11. Priya says:

    should it be called java access modifiers or java access specifiers?

  12. javaStarter says:

    good work with the table, just what i need

  13. Ajit jain says:

    nice job for beginner thanks………………..

  14. Srinivas says:

    Good job man… keep it up…

  15. Anonymous says:

    no access modifier – Subclasses in other package wont allow.

    But in protected will can be accessed subclasses in another pacakge.

  16. Vinayak says:

    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”.

  17. jatin says:

    its right answer i think

  18. Anonymous says:

    EXCELLENT THE WAY UR TEACHING

  19. Salem G. says:

    Thanks a lot for the explainig points on Java access modifiers…
    are very very nice fundamental article and easy

  20. ishq says:

    tanx guyz. i was in doubt abt friendly. no modifier=friendly na? tanx.

  21. Sekhar says:

    what is the exact difference between Java Access Specifiers and Java Access Modifiers?

    what is the type modifiers in Java?

  22. ankush says:

    Its very nice explanation..thnak you so much

  23. Joe says:

    @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.

Comments are closed for "Access Modifiers In Java".