Yes — since each can only be seen in the body of its method, each can be declared to be of any type.
It is sometimes useful to visualize methods as being surrounded by a box of "one-way glass." A method can see local variables and parameters that are inside the box. A method can look out through the glass that surrounds it. But no outsider can see into the box.
The picture shows the one-way glass for the
example program.
The red lines show the
box of one-way glass surrounding each method.
The method can see out of its box
(for example each method can see the
instance variable balance
)
but other methods can't see from the outside
into the box of one-way glass.
In processDeposit()
the statement
can see the variable balance
declared
as a instance variable.
It can also see the parameter amount
that is inside its box.
In processCheck()
statements
can see the instance variable balance
,
can see the parameter amount
,
and can see the local variable charge
.
The method showCharge()
is defective
because it contains a statement that attempts
to look inside the box that surrounds
processCheck()
.
The names of formal parameters (such as amount
)
and local variables (such as charge
)
are only visible from inside the glass box.
However,
outsiders can call the method and supply values for the formal parameters.
Is the name
of a method inside or outside
the glass box?