18
In HashMap class there is an inner class KeySet whose instance is returned by the HashMap's instance method keySet(). This inner class contains the following method.
public void clear() {
HashMap.this.clear();
}
I understand the general semantics of "this"
- It is a reference to the "current" object.
- Can be used within constructors or any instance methods where it acts as a reference to the object being constructed or the object whose methods are being invoked.
The above style of using "this" seems interesting,
- It is being used as if its a static variable of a class.
- It should still be referring to an object whose method is being invoked. In this logically it should be an HashMap instance.
Given the above two it should be possible to have a static reference to any dynamically created instance object but that's not possible since there could be infinite number of instances at runtime and there aren’t any language constructs to specify this relation between a class and its instances.
I am definitely missing something, can someone help me understand this better.