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.

«

»

Comments on “Access Modifiers In Java”

  1. MILICENT ANYAEGBU

    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.

    • Joe

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

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

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

  4. Priyank

    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. raj roy

    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. Satendra Dixit

    Thanks for the points on access modifiers…nice fundamental article

  7. VIRAT GANDHI

    nice work…….

  8. henok bekele

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

  9. henok bekele

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

  10. Priya

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

  11. javaStarter

    good work with the table, just what i need

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

  13. Srinivas

    Good job man… keep it up…

  14. Anonymous

    no access modifier – Subclasses in other package wont allow.

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

  15. Vinayak

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

  16. jatin

    its right answer i think

  17. Anonymous

    EXCELLENT THE WAY UR TEACHING

  18. Salem G.

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

  19. ishq

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

  20. Sekhar

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

    what is the type modifiers in Java?

  21. ankush

    Its very nice explanation..thnak you so much

  22. Joe

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