Java Reference
In-Depth Information
TIP:
(continued)
public
String getBalance()
{...}
...
}
//End of BankAccount
This invocation of
getBalance
is within the definition of the inner class
Money
. But the
inner class
Money
has no method named
getBalance
, so it is presumed to be the method
getBalance
of the outer class
BankAccount
.
But suppose the inner class did have a method named
getBalance
; then this invoca-
tion of
getBalance
would be an invocation of the method
getBalance
defined in the
inner class.
If both the inner and outer classes have a method named
getBalance
, then you can
specify that you mean the method of the outer class as follows:
public void
showBalance()
{
System.out.println(
BankAccount.
this
.getBalance());
}
The syntax
Outer_Class_Name
.
this
.
Method_Name
always refers to a method of the outer class. In the example,
BankAccount.this
means
the
this
of
BankAccount
, as opposed to the
this
of the inner class
Money
.
■
Self-Test Exercises
26. Consider the following class definition:
public class
OuterClass
{
public static class
InnerClass
{
public static void
someMethod()
{
System.out.println("From inside.");
}
}
Other_Members_of_OuterClass
}
Write an invocation of the static method
someMethod
that you could use in some
class you define.