2

Today when i was seeing the source code of the HashMap class in java i saw that the Entry class in HashMap whcih is used to record the key value pair is static.

 static class Entry<K,V> implements Map.Entry<K,V> {

}

As far i know that static member of the classes only have one value for all the instances of the class .How this phenomena is applicable in the case of static nested classes. I also read some discussions in the StackOverFlow can not come to exact point.Below is the link of some explanations that why the Entry class is static.

Static nested class in Java, why?

Community
  • 1
  • 1
Deepak
  • 2,287
  • 1
  • 23
  • 30

2 Answers2

2

As far i know that static member of the classes only have one value for all the instances of the class.

Correct.

How this phenomena is applicable in the case of static nested classes?

It isn't. A static nested class doesn't have an implied surrounding object, so it can't access the non-static members of the enclosing class.

The link you posted contains several answers all saying the same as above.

user207421
  • 305,947
  • 44
  • 307
  • 483
1

It's really just for organizational purposes. A static nested class behaves like a top-level class in a package. In this case, the Entry class is directly related to HashMap so it makes sense to nest it and not pollute the main package.

I would suggest reading up on the Oracle tutorial: http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html

telkins
  • 10,440
  • 8
  • 52
  • 79