Inner classes - a general question

i'm having some difficulties understanding when to use a static inner class

and when to use a non static inner class

lets say i'm implementing a LinkedList , and i want my nodes and my iterator

implemented in inner classes .

when and why should i declare them static or non static.

thank you very much in advance.

[352 byte] By [parallelya] at [2007-10-2 22:14:20]
# 1

Inner classes are never static. Nested classes can be either static or non-static; a non-static nested class is called an inner class.

Anyway, an inner class implicitly gets a reference to an object of the outer class, while a static nested class doesn't, so the decision should depend on whether or not it needs that.

An iterator would need a reference to its collection so it would be a non-static nested class. A node wouldn't so it would be a static nested class.

Lokoa at 2007-7-14 1:31:15 > top of Java-index,Java Essentials,Java Programming...
# 2
thank you.
parallelya at 2007-7-14 1:31:15 > top of Java-index,Java Essentials,Java Programming...
# 3

When you are coding iterators or other interface implementations, the standard way is to use private inner (that is, non-static nested) classes, or even local classes and just return the class as the interface. That hides implementation details from the program that is using your linked list or widget.

CliffsNotesa at 2007-7-14 1:31:15 > top of Java-index,Java Essentials,Java Programming...