Java Reference
In-Depth Information
Here the class
Inside
is nested inside the class
Outside
. The
Inside
class is declared as a public
member of
Outside
, so it is accessible from outside
Outside
. Obviously a nested class should have
some specific association with the enclosing class. Arbitrarily nesting one class inside another would not
be sensible. The enclosing class here is referred to as a
top-level class
. A top-level class is a class that
contains a nested class but is not itself a nested class.
Our nested class here only has meaning in the context of an object of type
Outside
. This is because
Inside
is not declared as a static member of the class
Outside
. Until an object of type
Outside
has been
created, you can't create any
Inside
objects. However, when you declare an object of a class containing a
nested class, no objects of the nested class are necessarily created - unless of course the enclosing class's
constructor creates them. For example, suppose we create an object with the following statement:
Outside outer = new Outside();
No objects of the nested class,
Inside
, are created. If you now wish to create an object of the type of
the nested class you must refer to the nested class type using the name of the enclosing class as a
qualifier. For instance, having declared an object of type
Outside
, we can create an object of type
Inside
as follows:
Outside.Inside inner = outer.new Inside(); // Define a nested class object
Here we have created an object of the nested class type that is associated with the object,
outer
,
created earlier. We are creating an object of type
Inside
in the context of the object
outer
. Within
non-static methods that are members of
Outside
, you can use the class name
Inside
without any
qualification as it will be automatically qualified by the compiler with the
this
variable. So we could
create a new
Inside
object from within the method of the object
Outside
:
Inside inner = new Inside(); // Define a nested class object
which is equivalent to:
this.Inside inner = this.new Inside(); // Define a nested class object
All this implies that a static method cannot create objects of a non-static nested class type. Because the
Inside
class is not a static member of the
Outside
class, such a member could refer to an object which
does not exist - an error, if there are no
Inside
objects extant in the context of an
Outside
object. And as
Inside
is not a static data member of the
Outside
class, if a static method in the
Outside
class tried to
create an object of type
Inside
directly, without first invoking an object of type
Outside
, it would be
trying to create an object outside of that object's legitimate scope - an illegal maneuver.
Further, because the class
Inside
is not a static member of the
Outside
class, it cannot in turn
contain any static data members itself. Since
Inside
is not static, it cannot act as a freestanding class
with static members - this would be a logical contradiction.
Nested classes are typically used to define objects that at least have a strong association with objects of
the enclosing class type, and often there is a tight coupling between the two. A further use for nested
classes is for grouping a set of related classes under the umbrella of an enclosing class. We will be using
this approach in examples later on in the topic.