0

Possible Duplicate:
Static nested class in Java, why?

I'm reading sourcecode in JDK HashTable, and there is some code snippet like below:

/** Hashtable collision list. */

private static class Entry<K,V> implements Map.Entry<K,V> {
    int hash;
    K key;
    V value;
    Entry<K,V> next;
    ...
} //end of Entry<K,V>

I know the Entry here is a private inner class, but it's also a defined static class; my concern is, it isn't enough to just define it as private inner class? What's the main usage here to define it as a static class?

Any guide or reply is highly appreciated.

Community
  • 1
  • 1
Vincent Jia
  • 592
  • 6
  • 14

1 Answers1

3

It is static so that its instances do not contain a reference to the containing instance of the outer class.

Map.Entry is used to iterate through the elements of the map. It is needless for it to have a reference to the containing map, as it needs no access to the private fields of the map. So declaring it non-static would just use up extra space and weaken encapsulation.

Péter Török
  • 114,404
  • 31
  • 268
  • 329