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.
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.
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.
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
.