| private | member | Accessible only in its class(which defins it). |
| protected | member | Accessible only within its package and its subclasses |
| public | class interface member | Accessible anywhere Accessible anywhere Accessible anywhere its class is. |
(Taken from http://www.javacamp.org/javaI/modifier.html)
Let us create a new class named "Student".
public class Student {
private int score;
public Student() {
score = 89;
}
public void modifyScore(Student that,int newValue){
that.score = newValue;
}
public String toString() {
return "Score = " + score;
}
}
Imagine a situation where instance "A" of class "Student" trying to modify the "score" member of instance "B" of the same class, The misunderstanding is this cannot be done because of the score's access modifier is private. In fact, it can be done correctly. As the rule states that private members are accessible only in its class. So instances could access the private members of other instances too.
