Java Reference
In-Depth Information
class can also access the private members of the inner class, but only
by an explicit reference to an inner class object such as lastAct . While
an object of the inner class is always associated with an object of the
enclosing class, the converse is not true. An object of the enclosing class
need not have any inner class objects associated with it, or it could have
many.
When deposit creates an Action object, a reference to the enclosing
BankAccount object is automatically stored in the new Action object. Using
this saved reference, the Action object can always refer to the enclos-
ing BankAccount object's number field by the simple name number , as shown
in toString . The name of the reference to the enclosing object is this
preceded by the enclosing class namea form known as qualified- this .
For example, toString could reference the number field of the enclosing
BankAccount object explicitly:
return BankAccount.this.number + ": " + act + " " + amount;
The qualified- this reference reinforces the idea that the enclosing object
and the inner object are tightly bound as part of the same implement-
ation of the enclosing class. This is further reinforced by the quali-
fied- super reference, which allows access to members of the enclosing
instance's superclass that have been hidden, or overridden, by the en-
closing class. For example, given a class T that extends S , within T we
can invoke the superclass implementation of a method m , by using su-
per.m() in an expression. Similarly, in an inner class of T , we can invoke
the same implementation of m using T.super.m() in an expressiona quali-
fied- super referenceand similarly for fields of S hidden by fields in T .
A nested class can have its own nested classes and interfaces. Referen-
ces to enclosing objects can be obtained for any level of nesting in the
same way: the name of the class and this . If class X encloses class Y
which encloses class Z , code in Z can explicitly access fields of X by using
X.this .
 
Search WWH ::




Custom Search