Nested Class

A class within a class.

Supported in some languages:

we haven't yet found the right words to describe the relationship of nested classes: it isn't 'is-a' or 'has-a'. What criteria should be applied when choosing whether to have a class be defined entirely within another class? I have some vague gut-level feelings about this, but haven't come up with anything substantial... -- Anonymous

I think of NestedClasses as being mostly useful for helping organize the namespace. I find myself writing a lot of MethodObjects in them, but instead of ShoppingCartCalculator I can simply call it Calculator, and wrap it inside of the ShoppingCart class to keep it distinct from other sorts of Calculators in the system. -- Another Anonymous

In a language which allows the nested class access to a specific instance of the outer class (i.e., java), it's close to a 'is-a' or 'has-a', except it's reversed: the nested class is the one that 'has' or 'is' the outer class: consider the following example:

 class Foo{
public Foo(){
SomeService.addListener(new ResetFoo());
}

public static class ResetFoo extends Listener{ public void eventOccured(){ this.Foo.reset(); } }

public void reset(){ // do whatever 'reset' means in this context }

// methods, etc... }
It's not the Foo instance which 'has-a' ResetFoo, although it could if it wants to retain a reference to its listener (which is a whole other ball of wax), but the ResetFoo which 'has-a' Foo. In other words, nested class are a way of 'being-had', if that makes any sense. :)

It's interesting, in that a client is freed from having to maintain a specific reference to the Foo class. Doesn't sound particularily interesting, but consider that the said client could arrange to supply, register, contribute, manage, etc., the services of Foo without any knowledge of what the heck a Foo is (what stops SomeService from contributing the service of ResetFoo to others?)

-- Another mess from the friendly naive WilliamUnderwood :)

In terms of the nested "static" class concept, the NestedClass is entirely syntactic sugar... unless the system supports reflection. In CsharpLanguage this is not insignificant - many crucial C# tools depend on reflection. I ran into this problem when I decided to make my configuration class nesting tree mirror the composition tree and found that the dotnet XML serialization tools had subtle problems with the nested classes.

In languages supporting the "inner class" idiom, the nested classes are actually useful, particularly where you have a factory concept in which the factories and constructs are all of the same (or similar) type but with different parameters. This is paricularly relevant in the case of user design tools, where you have user-defined templates and user-defined instances of the template - the template is the outer class, the instance is the inner class. In general, inner classes are very useful when you need the app to be able to provide it's own class->instance functionality, with defined objects in the role of classes. Instead of "is a" or "has a" think "constructs (and owns)" where (and owns) is relevant if the factory class is mutable. --MartinZarate


"A NestedClass is any class whose declaration occurs within the body of another class or interface. A top level class is a class that is not a nested class." -- JavaLanguageSpecification, Second Edition (http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html)

Unfortunate wording; seem to be generally referred to as InnerClass everywhere but here.

Editorial note: this page was originally just the above Java quote, but I thought we needed a generic term for Java's inner classes and C++'s member classes.


EditText of this page (last edited October 12, 2006) or FindPage with title or text search