Access Modifiers In Java

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

Ads by Google

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.

This Core Java tutorial was added on 22/05/2008.

previous post: How to avoid IllegalStateException in java servlet?

next post: Java Final Keyword

23 comments on “Access Modifiers In Java

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

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

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

  4. 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?

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

  6. Thanks for the points on access modifiers…nice fundamental article

  7. nice work…….

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

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

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

  11. good work with the table, just what i need

  12. Good job man… keep it up…

  13. no access modifier – Subclasses in other package wont allow.

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

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

  15. its right answer i think

  16. EXCELLENT THE WAY UR TEACHING

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

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

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

    what is the type modifiers in Java?

  20. Its very nice explanation..thnak you so much

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