-5

I am creating a class named MyList, and declare all instance variables to be private. In my client code class here named "MyListClient", I declared object list1, of course, MyListClient cannot directly access to private instance variable size by list1.size, the only way I can do is by calling method list1.size().

However, in the implementation class named "MyList", for the method addAll, why can I access to another object's private instance variable. I attached screenshot here, in the addAll method, "other" is the different object of the same class "MyList". But I can write code, like "other.size" or "other.elementData", there are no error happened. Do I miss something? I think you cannot access to another object's private instance variable, but here you can. In the client code class, "List1" is also the different object of same class MyList, which cannot write like "list1.size", I am so confused. enter image description here enter image description here I thought it is wrong to write code like "other.size" and "other.elementData", the error should happen. However it is correct code. I cannot understand why

  • 6
    `private` is not private to the instance, it's private to the class. – shmosel Aug 17 '23 at 17:11
  • 1
    It's explained in the docs - [Access Control](https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html). To cite - `The private modifier specifies that the member can only be accessed in its own class.`, you are inside the class, so naturally you have access to it. – Chaosfire Aug 17 '23 at 17:12
  • 3
    "I think you cannot access to another object's private instance variable" - what part of the Java Language Specification leads you to think that? See [JLS 6.6.1](https://docs.oracle.com/javase/specs/jls/se20/html/jls-6.html#jls-6.6.1) for what private actually means. – Jon Skeet Aug 17 '23 at 17:13
  • 2
    As an aside, please always provide code as *text* rather than images on Stack Overflow - and a minimal example is generally better too. (This can be demonstrated in very few lines of code... [example](https://gist.github.com/jskeet/d76de3a756191ebb515550dc4d77cd21)) – Jon Skeet Aug 17 '23 at 17:14
  • what does it mean by private to class, not to private to the instance? "MyList" is a class, "other.size" is accessing to the private variable "size" which belongs to the different object "other", other.size is not the size of current implementing class "MyList", if it is private to class, the size inside of object "other" should be private to the current class "MyList". I am still confused. I understand you can access the private instance variable in the "own declared class", like public int size() { return size; }, Oracle documentation is more confusion. – user3559982 Aug 17 '23 at 18:05
  • 2
    "Different object" is where you're going wrong. It's a different object, but it's the same class. Any MyList can access any other MyList's private fields. They're both instances of MyList. – Louis Wasserman Aug 17 '23 at 18:44

1 Answers1

0

The private access modifier makes it so that only the class that it is declared can access it, the addAll method is declared within your myList class itself, which means you can access other.elementData[i]

===============================================================

Side Note (Remember that this is not necessary and is personal preference)

Instead of using -> for (int i = 0; ...) Use a for each loop like so ->

for(int a : other.elementData) {
   add(a);
}
  • In the current declared class "MyList", the other.elementData is accessing to object "other" private instance variable "elementData", which is not the same as the "elementData" (line 6 of the second screenshot)in the current implementing class "MyList", I cannot understand this part. Thank you for the "for loop" suggestion. I like that. – user3559982 Aug 17 '23 at 18:20
  • 1
    I clearly said it. The `private` access modifier makes it so that **ONLY THE CLASS** can access it. Now the `addAll` method is within the `MyList` class? It doesn't matter which object it is, until it is accessed within the class it is not a problem. – Grinding For Reputation Aug 17 '23 at 18:28
  • 2
    @user3559982 in other words, private means that only the code from the same class can access that member - "same class" not to be confused with "same instance" – user16320675 Aug 17 '23 at 19:13