To generate a handle to an external class object, you use a dot number and a this to name the outer class. For example, in the Sequence.sselector class, all of its methods can produce a stored handle to an external class sequence, using the form of sequence.this. The resulting handle will automatically have the correct type (this is checked and verified during compilation, so there is no runtime overhead).
Sometimes we want to tell some other objects to create an object of one of its inner classes. To do this, you must provide a handle to other external class objects in the new expression, as follows:
: Parcel11.java
//Creating inner Classes
package c07.parcel11;
public class Parcel11 {
class Contents} {
private int i = one;
public int value () {return i;}
}
Class Destination {
private String label;
Destination (String whereto) {
label = Whereto;
}
String Readlabel () {return label;}
}
public static void Main (string[] args) {
Parcel11 p = new Parcel11 ();
Must use instance of Outer class
//To create a instances of the inner class:
parcel11.contents c = p.new Co Ntents ();
Parcel11.destination d =
p.new destination ("Tanzania");
}
///:~
To create an object for an inner class directly, you cannot use the same form as you might guess, and reference the external class name PARCEL11. At this point, an object of an inner class must be generated from an object of an external class:
Parcel11.contents C = p.new Contents ();
Therefore, it is not possible to create an object of an inner class unless you already have an object of an external class. This is because the object of the inner class has been "silently" connected to the object of the external class that created it. However, if you generate a static inner class, you do not need to point to a handle to the external class object.