WebReference.com - Part 5 of chapter 5 from Beginning Java 2 SDK 1.4 Edition, Wrox Press Ltd (1/6)
[next] |
Beginning Java 2 SDK 1.4 Edition
Nested Classes
[The following is the conclusion of our series of excerpts from chapter 5 of the Wrox Press title, Beginning Java 2 SDK 1.4 Edition. Source code for the examples discussed can be downloaded at the Wrox Web site (free e-mail registration required).]
All the classes we have defined so far have been separate from each other--each stored away in its own file. Not all classes have to be defined like this. You can put the definition of one class inside the definition of another class. The inside class is called a nested class. A nested class can itself have another class nested inside it, if need be.
When you define a nested class, it is a member of the enclosing class in much the same way as other members. A nested class can have an access attribute just like other class members, and the accessibility from outside the enclosing class is determined by the attributes in the same way.
public class Outside {
// Nested class
public class Inside {
// Details of Inside class...
}
// More members of Outside class...
}
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 book.
[next] |
Created: August 5, 2002
Revised: August 5, 2002
URL: https://webreference.com/programming/java/beginning/chap5/5/