I know that an inner class has access to everything in the outer class (because it\'s a member of that class) but what about the other way around?
Does the
This topic is covered in some detail by Effective Java (2nd edition) Item 22: "Favor static member classes over nonstatic".
A brief summary:
static
by default. To get technical, Effective Java calls these static member classes, as opposed to inner classes, and uses the term nested class to encompass both the static and nonstatic versions.private
. In this way, an inner class can expose itself only to its outer class.Personally, I'm inclined to implement an inner class whenever doing so allows the inner class's constructors to be private
, i.e. when a class can only be instantiated from one other (outer) class. Any additional encapsulation, such as making the entire inner class private
, is desirable; but public
inner classes are perfectly acceptable. There are many examples in Java, such as AbstractMap.SimpleEntry.