Java Reference
In-Depth Information
The file drawer for a static nested class
In our model of execution, each class has a file drawer in a filing cabinet.
By our rules, static components of a class are placed in the file drawer for the
class. Since static class
Line
is defined inside class
WireForm
, its filing cabinet
belongs inside
WireForm
's filing cabinet —we have to cram one file drawer
inside another! Figure 12.12 shows the situation, with the filing cabinet drawers
drawn as boxes. The file drawer for class
Line
shows one instance of the class,
and there are no static components.
Above, we mentioned the general inside-out rule, which is discussed in Sec.
13.1.2. Applied to a static nested class and in terms of our file-drawer model, the
inside-out rule is interpreted as follows:
Activity
12-5.2
Inside-out rule for static nested classes
. Suppose the file draw-
er for static nested class
In
is inside another file drawer
Out
. Then
In
's methods can reference each static component
x
of
Out
direct-
ly (unless
In
redefines
x
).
All instances of
Line
are in
Line
's file drawer, so they can reference
x
.
12.4.2
Inner classes
An
inner class
In
(say) is a class that is defined within another class
Out
(say)
without modifier
static
. Class
In
could be defined as a component of
Out
, or
it could be defined within a method. Here, we discuss the case that class
In
is
defined as a component of class
Out
, and not within a method.
We describe the use of an inner class using the following example. Class
BankAccount
, outlined in Fig. 12.13, is used to maintain bank accounts. Each
instance of the class maintains the account number and the balance. There are
methods for depositing an amount and withdrawing an amount. There could be
other types of transactions as well —adding interest, charging a service fee, and
Activity
12-6.1
WireForm
's file drawer
Line
's file drawer
x
...
a1
Line
a0
WireForm
x1
..
y1
..
z1 ..
y
...
meth(...)
y2
..
x2
..
z2
..
Figure 12.12:
The file drawer for a static nested class
Search WWH ::
Custom Search