Java Reference
In-Depth Information
Every method has one implicit parameter. You don't give the implicit parameter a
name. It is always called this . (There is one exception to the rule that every method
has an implicit parameter: static methods do not. We will discuss them in Chapter
8 .) In contrast, methods can have any number of explicit parametersȌwhich you can
name any way you likeȌor no explicit parameter at all.
Next, look closely at the implementation of the deposit method. The
statement
double newBalance = balance + amount;
actually means
double newBalance = this.balance + amount;
When you refer to an instance field in a method, the compiler automatically applies it
to the this parameter. Some programmers actually prefer to manually insert the
this parameter before every instance field because they find it makes the code
clearer. Here is an example:
Use of an instance field name in a method denotes the instance field of the implicit
parameter.
public void deposit(double amount)
{
double newBalance = this.balance + amount;
this.balance = newBalance;
}
You may want to try it out and see if you like that style.
You have now seen how to use objects and implement classes, and you have learned
some important technical details about variables and method parameters. In the next
chapter, you will learn more about the most fundamental data types of the Java
language.
Search WWH ::




Custom Search