Why can I access my private variables of the “other” object directly, in my equals(Object o) method

后端 未结 2 632
自闭症患者
自闭症患者 2020-11-30 05:01

In Java in the equals(Object o) method I can access the private variables of the passed in object without going through its public getters.

public boolean eq         


        
2条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-30 05:02

    Private data is accessible by any instance of that class, even if one instance of class A is accessing the private members of another instance of A. It's important to remember that that access modifiers (private, protected, public) are controlling class access, not instance access.

提交回复
热议问题