Java Fields Accessibility (and not Visibility!!!) / Public – Private – Protected ….

For a long time, my understanding of Java fields accessibility was wrong !!!

Here are the right accessibility for public, private, protected and package fields

private : can be accessed only from the class declaring the field

public : can be accessed from everywhere

package (when no visibility is specified) : can be accessed from every class within the same package

protected : can be accessed from any subclass OR from any class within the same package (protected = package + subclasses)

4 Responses to “Java Fields Accessibility (and not Visibility!!!) / Public – Private – Protected ….”

  1. Philip Says:

    Very helpful and to-the-point,
    thanks

  2. Manuel Says:

    Happy to know some of my posts are useful for others ;)
    Thanks for your comment

  3. kay Says:

    thanx thanx love the brevity

  4. RcK17 Says:

    Thanks :)

Leave a Reply