Nested classes

Here is another advantage of the Java, an object-oriented programming language that allows us to define
a class within another class, such classes are known as nested classes. Inner
classes can be either named or anonymous. We'll discuss about anonymous classes later in this section. A named inner class looks just like
any other class, except that it is nested inside another class. ( It can
even contain further levels of nested classes, but you shouldn't carry these
things too far ).
Like the members of a class, a named inner class
can be either static or non-static. A static nested class is part of the
static structure of the containing class. It can be used inside a class that
is used to create objects in the usual way. If it has not been declared private,
then it can also be used outside the containing class, but when it is used
outside the class, its name must indicate its membership in the containing
class. This is similar to other static components of a class: A static
nested class is part of the class itself in the same way as static member
variables are the part of the class itself.
A
simple nested class example is given below:
class OuterClass {
...expression...
class NestedClass {
...expression...
}
} |
A static nested class has full access to the
members of the containing class, even to the private members. This can be
another motivation for declaring a nested class, since it lets a class to
gain access to the private members of another class without making those
members generally available to other classes.
There are two categories of nested classes, which
are as under:
i) Static classes
ii) Inner classes (Non-static)

|