您的位置:寻梦网首页编程乐园Java天地Core JavaJava Lecture Notes
Introduction
Content
Apply
Reflect
Extend
previous.gif
 (3087 bytes)
next.gif
 (2959 bytes)

 
 
Content Index
Content Page # 13

Arguments (parameters)

Arguments can be though of as the `input' to methods.

By `input' is meant an input from another part of the program, not from the user. Arguments, like variables, have a name and a type.

Suppose that when a customer pays some money into an account, it increases the account balance. We want to define a method called payIn() which is called whenever the customer pays some money in. This method cannot add a fixed amount to the balance, as it must be able to cope with any amount of money paid in. For example, we would like to be able to write

payIn (300);
to pay in 300 pounds, and
payIn (10000);
to pay in 10,000 pounds, etc. The number in parentheses (brackets) is called an argument of this method. We can define the payIn() method like this:
public void payIn (int amount)
{
      accountBalance = accountBalance + amount;
}
Don't forget that accountBalance is the name of the variable that stores the account balance for this customer.

Again, the name of the argument `amount' is only meaningful inside the method. We could have used any name for this argument (provided all instances were the same) and it would work the same. Of course, common sense suggests that you should choose identifiers that make the program easier to understand.

Back to top

basicline.gif (169 bytes)

RITSEC - Global Campus
Copyright © 1999 RITSEC- Middlesex University. All rights reserved.
webmaster@globalcampus.com.eg