Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HashMap.this.clear() what does this mean, how does this work

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"

  1. It is a reference to the "current" object.
  2. 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,

  1. It is being used as if its a static variable of a class.
  2. 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.

like image 443
sandeepkunkunuru Avatar asked Jun 08 '13 12:06

sandeepkunkunuru


People also ask

What does clear do in HashMap?

HashMap. clear() method in Java is used to clear and remove all of the elements or mappings from a specified HashMap. Parameters: The method does not accept any parameters. Return Value: The method does not return any value.

What does Clear () do in Java?

clear() method is used to remove all the elements from a Set. Using the clear() method only clears all the element from the set and not deletes the set. In other words, we can say that the clear() method is used to only empty an existing Set. Return Value: The method does not returns any value.

What is the difference between Clear () and remove () method of HashMap?

We can remove entries from HashMap using remove(key) or clear() methods. remove method removes the mapping for the key specified in the parameter while clear() method removes all the entries from the HashMap and returns void.

Do we need to clear HashMap in Java?

If all you want to do is discard the data in the Map , then you need not (and in fact should not) call clear() on it, but simply clear all references to the Map itself, in which case it will be garbage collected eventually. Show activity on this post. Looking at the source code, it does look like HashMap never shrinks.


2 Answers

When a non-static inner class is created, it gets a reference to its enclosing class instance. In order to reference that instance, a special syntax is used: the keyword this is prefixed with the name of the enclosing class. When used without a class name prefix, this keyword refers to the instance of the inner class itself.

Essentially, the call is made to the clear method of the HashMap class. Note that since the method of the inner class is called clear as well, the call without HashMap.this would have been directed to the clear() inside the inner class, causing infinite recursion.

As correctly stated by Roger Lindsjö in a comment below Technically the idiom is known as qualified this construct. (JLS 15.8.4)

like image 76
Sergey Kalinichenko Avatar answered Sep 26 '22 20:09

Sergey Kalinichenko


HashMap.this.clear(); calls the current instance of HashMap, you mentioned that its calling from a InnerClass, so that's how its referencing the Outter class.

like image 36
DevZer0 Avatar answered Sep 22 '22 20:09

DevZer0