Can anybody explain me why below code is working on private member variable?
public class Person implements Comparable<Person> {
private String firstName;
public Person(String firstName) {
this.firstName = firstName;
}
@Override
public int compareTo(Person o) {
return firstName.compareToIgnoreCase(o.firstName); // why does it work? } }
}
}
EDIT Why o.firstName is getting compile ? where firstName is private variable.
The access modifiers control access per class rather than per instance. So, methods of class
Tcan access all members (even private) of other classT‘s instances.The Access Control from JLS 7 link for the curious. It says, “Note that accessibility is a static property that can be determined at compile time; it depends only on types and declaration modifiers.”