Reputation: 53
I developed my own small implementation of LinkedList without using Node nested class as static and it is working fine.... But Java Uses Node nested class as static.I didn't find a strong reason to make Node nested class as static.
Can anyone help me in explaining this?
Upvotes: 2
Views: 2880
Reputation: 1225
A nested class is a member of its enclosing class.Non-static nested classes (inner classes) have access to other members of the enclosing class, even if they are declared private.Static nested classes do not have access to other members of the enclosing class.
A static nested class interacts with the instance members of its outer class (and other classes) just like any other top-level class. In effect, a static nested class is behaviorally a top-level class that has been nested in another top-level class for packaging convenience.
There is no need for LinkedList.Node to be top-level class as it is only used by LinkedList.And since it does not need access to LinkedList's members, it makes sense for it to be static - it's a much cleaner approach.
Upvotes: 1