Java Reference
In-Depth Information
// More members of Outside class...
}
Here the class
Inside
is nested inside the class
Outside
. The
Inside
class is declared as a public mem-
ber of
Outside
, so it is accessible from outside
Outside
. Obviously, a nested class should have some spe-
cific 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.
The nested class here has meaning only in the context of an object of type
Outside
. This is because the
Inside
class 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 contain-
ing 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 you create an object with the following statement:
Outside outer = new Outside();
No objects of the nested class,
Inside
, are created. If you now want 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
, you can create an object of type
Inside
as follows:
Outside.Inside inner = outer.new Inside(); // Define a nested class object
Here you have created an object of the nested class type that is associated with the object
outer
that you
created earlier. You 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 is automatically qualified by the compiler with the
this
variable. So you could create a new
Inside
object from within a method of the object
Outside
:
Inside inner = new Inside(); // Define a nested class object
This statement 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 that does
not exist — which would be an error if there are no
Inside
objects extant in the context of an
Outside
object. Because
Inside
is not a static 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
Inside
class is not a static member of the
Outside
class, it cannot in turn contain
any static data members itself. Because
Inside
is not static, it cannot act as a freestanding class with static
members — this would be a logical contradiction.
You typically use nested classes 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. You use this approach in
examples later on in the topic.