Site hosted by Angelfire.com: Build your free website today!
 

Nested and Inner Class

You use nested classes to reflect and enforce the relationship between two classes. You should define a class within another class when:

  • the nested class makes sense only in the context of its enclosing class, or

  • when it relies on the enclosing class for its function.

For example, a text cursor makes sense only in the context of a particular text component.

As a member of its enclosing class, a nested class has a special privilege: It has unlimited access to its enclosing class's members, even if they are declared private.

Like other members, a nested class can be declared static (or not). A static nested class is called just that: a static nested class. A nonstatic nested class is called an inner class. These are illustrated in the following code:

class EnclosingClass{
    . . .
    static class AStaticNestedClass {
        . . .
    }
    class InnerClass {
        . . .
    }
}

See: Nested Class, Inner Class