Java Fields Visibility / Public - Private - Protected ….

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

Here are the right visibilities 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)

Leave a Reply