0

Can someone explain the way of using this in AbstractList sources: AbstractList.this.remove(lastRet);

This line is in the remove() method of the private class Itr implements Iterator<E> inner class.

Laurentiu L.
  • 6,566
  • 1
  • 34
  • 60
serhii
  • 1,135
  • 2
  • 12
  • 29

2 Answers2

1

The syntax ClassName.this works in nested non-static classes. It lets you access the reference to the object that "owns" the nested class, i.e. this object of the parent object.

This is the case of the iterator class inside the abstract list: it calls remove method on its parent AbstractList object.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

Inner classes (as opposed to static nested classes) are always associated with an instance of the surrounding class.

Since they are members of the surrounding class, they have access to all its members, private or public. In order to access a member of the surrounding class, especially if the member name may be ambiguous, you need to access the "this" of the surrounding class instance.

The syntax used is the name of the surrounding class with .this. Which is exactly what you see in your example. AbstractList is the surrounding class, and AbstractList.this is the instance of the surrounding class associated with the current instance of Itr.

RealSkeptic
  • 33,993
  • 7
  • 53
  • 79