WebReference.com - Part 5 of chapter 5 from Beginning Java 2 SDK 1.4 Edition, Wrox Press Ltd (4/6)
[previous] [next] |
Beginning Java 2 SDK 1.4 Edition
Using a Nested Class outside the Top-Level Class
You can create objects of an inner class outside the top-level class containing the inner class.
As we discussed, how you do this depends on whether the nested class is a static member of the
enclosing class. With the first version of our MagicHat
class, with a static Rabbit
class, you could create an independent rabbit by adding the following statement to the end of main()
:
System.out.println("An independent rabbit: " + new MagicHat.Rabbit());
This Rabbit
object is completely free--there is no MagicHat
object to restrain
it. In the case of a non-static Rabbit
class, things are different. Let's try this using a
modified version of the previous program.
Try It Out--Free Range Rabbits (Almost)
We can see how this works by modifying the method main()
in TryNestedClass
to create another MagicHat
object, and then create a Rabbit
object for it:
static public void main(String[] args) {
// Create three magic hats and output them
System.out.println(new MagicHat("Gray Topper"));
System.out.println(new MagicHat("Black Topper"));
System.out.println(new MagicHat("Baseball Cap"));
MagicHat oldHat = new MagicHat("Old hat"); // New hat object
MagicHat.Rabbit rabbit = oldHat.new Rabbit(); // Create rabbit object
System.out.println(oldHat); // Show the hat
System.out.println("\nNew rabbit is: " + rabbit); // Display the rabbit
}
The output produced is:
Gray Topper contains:
Thumper1
Black Topper contains:
Moppsy1 Thumper2 Thumper3
Baseball Cap contains:
Floppsy1 Floppsy2 Thumper4
Old hat contains:
Floppsy3 Thumper5 Thumper6 Thumper7 Thumper8
New rabbit is: Thumper9
How it Works
The new code first creates a MagicHat
object, oldHat
. This will have its own
rabbits. We then use this object to create an object of the class MagicHat.Rabbit
. This is
how a nested class type is referenced--with the top-level class name as a qualifier. You can only call
the constructor for the nested class in this case by qualifying it with a MagicHat
object
name. This is because a non-static nested class can refer to members of the top-level class--including
instance members. Therefore, an instance of the top-level class must exist for this to be possible.
Note how the top-level object is used in the constructor call. The object name qualifier goes before
the keyword new which precedes the constructor call for the inner class. This creates an object,
rabbit
, in the context of the object oldHat
. This doesn't mean oldHat
has rabbit as a member. It means that, if top-level members are used in the inner class, they will be the
members for oldHat
. You can see from the example that the name of the new rabbit is not part
of the oldHat
object, although it is associated with oldHat
. You could
demonstrate this by modifying the toString()
method in the Rabbit
class to:
public String toString() {
return name + " parent: "+hatName;
}
If you run the program again, you will see that when each Rabbit
object is displayed,
it will also show its parent hat.
Local Nested Classes
You can define a class inside a method--where it is called a local nested class. It is also referred to as a local inner class, since a non-static nested class is often referred to as an inner class. You can only create objects of a local inner class locally--that is, within the method in which the class definition appears. This is useful when the computation in a method requires the use of a specialized class that is not required or used elsewhere.
A local inner class can refer to variables declared in the method in which the definition appears,
but only if they are final
.
[previous] [next] |
Created: August 5, 2002
Revised: August 5, 2002
URL: https://webreference.com/programming/java/beginning/chap5/5/4.html