By Barry Burd

To understand how to pass values to methods in Java, think about sending someone to the supermarket to buy bread. When you do this, you say, “Go to the supermarket and buy some bread.” Some other time, you send that same person to the supermarket to buy bananas. You say, “Go to the supermarket and buy some bananas.” And what’s the point of all this?

Well, you have a method, and you have some on-the-fly information that you pass to the method when you call it. The method is named “Go to the supermarket and buy some….” The on-the-fly information is either “bread” or “bananas,” depending on your culinary needs. In Java, the method calls would look like this:

goToTheSupermarketAndBuySome(bread);
goToTheSupermarketAndBuySome(bananas);

The things in parentheses are called parameters or parameter lists. With parameters, your methods become much more versatile. When you call your goToTheSupermarketAndBuySome method, you decide right there and then what you’re going to ask your pal to buy.

This is how you would add interest.

import java.text.NumberFormat;
import static java.lang.System.out;
class NiceAccount {
 String lastName;
 int id;
 double balance;
 void addInterest(double rate) {
  out.print("Adding ");
  out.print(rate);
  out.println(" percent...");
  balance += balance * (rate / 100.0);
 }
 void display() {
  NumberFormat currency =
   NumberFormat.getCurrencyInstance();
  out.print("The account with last name ");
  out.print(lastName);
  out.print(" and ID number ");
  out.print(id);
  out.print(" has balance ");
  out.println(currency.format(balance));
 }
}

Now, you have to call the addInterest Method.

import java.util.Random;
class ProcessNiceAccounts {
 public static void main(String args[]) {
  Random myRandom = new Random();
NiceAccount anAccount;
  double interestRate;
  for (int i = 0; i < 3; i++) {
   anAccount = new NiceAccount();
   anAccount.lastName = " +
    (char) (myRandom.nextInt(26) + 'A') +
    (char) (myRandom.nextInt(26) + 'a') +
    (char) (myRandom.nextInt(26) + 'a'); 
   anAccount.id = myRandom.nextInt(10000);
   anAccount.balance = myRandom.nextInt(10000);
   anAccount.display();
   interestRate = myRandom.nextInt(5);
   anAccount.addInterest(interestRate);
   anAccount.display();
   System.out.println();
  }
 }
}

In Listing 19-7, the line

anAccount.addInterest(interestRate);

plays the same role as the line goToTheSupermarketAndBuySome(bread) in this little supermarket example. The word addInterest is a method name, and the word interestRate in parentheses is a parameter. Taken as a whole, this statement tells the code to execute its addInterest method.

This statement also tells Java to use a certain number in the method’s calculations. The value of interestRate can be 1.0, 2.0, or whatever other value you get by calling myRandom.nextInt(5). In the same way, the goToTheSupermarketAndBuySome method works for bread, bananas, or whatever else you need from the market.

image0.jpg

Java has very strict rules about the use of types. For example, you can’t assign a double value (like 3.14) to an int variable. But Java isn’t completely unreasonable about the use of types.

Java allows you to assign an int value (like myRandom.nextInt(5)) to a double variable (like interestRate). If you assign the int value 2 to the double variable interestRate, then the value of interestRate becomes 2.0.

Handing off a value

When you call a method, you can pass information to that method on the fly. This information is in the method’s parameter list. You have to add a call to the addInterest method:

anAccount.addInterest(interestRate);

The first time through the loop, the value of interestRate is 2.0. So at that point in the program’s run, the method call behaves as though it’s the following statement:

anAccount.addInterest(2.0);

The computer is about to run the code inside the addInterest method. But first, the computer passes the value 2.0 to the parameter in the addInterest method’s header. So inside the addInterest method, the value of rate becomes 2.0.

image1.jpg

Here’s something interesting. The parameter in the addInterest method’s header is rate. But, inside the ProcessNiceAccounts class, the parameter in the method call is interestRate. That’s okay. In fact, it’s standard practice.

The names of the parameters don’t have to be the same. The only thing that matters is that both parameters (rate and interestRate) have the same type. Both of these parameters are of type double. So everything is fine.

Inside the addInterest method, the += assignment operator adds balance * (rate / 100.0) to the existing balance value.

Working with a method header

Here are some observations about the addInterest method header:

  • The word void tells the computer that when the addInterest method is called, the addInterest method doesn’t send a value back to the place that called it.

  • The word addInterest is the method’s name.

    That’s the name you use to call the method when you’re writing the code for the ProcessNiceAccounts class.

  • The parentheses in the header contain placeholders for all the things you’re going to pass to the method when you call it.

    When you call a method, you can pass information to that method on the fly. This information is the method’s parameter list. The addInterest method’s header says that the addInterest method takes one piece of information, and that piece of information must be of type double:

    void addInterest(double rate)

    Sure enough, if you look at the call to addInterest (down in the ProcessNiceAccounts class’s main method), that call has the variable interestRate in it. And interestRate is of type double. When you call getInterest, you’re giving the method a value of type double.

How the method uses the object’s values

The addInterest method is called three times from the main method. The actual account balances and interest rates are different each time:

  • In the first call, the balance is 8983.00, and the interest rate is 2.0.

    When this call is made, the expression balance * (rate / 100.0) stands for 8983.00 * (2.0 / 100.00).

    image2.jpg

  • In the second call, the balance is 3756.00, and the interest rate is 0.0.

    When the call is made, the expression balance * (rate / 100.0) stands for 3756.00 * (0.0 / 100.00).

  • In the third call, the balance is 8474.00, and the interest rate is 3.0.

    When the addInterest call is made, the expression balance * (rate / 100.0) stands for 8474.00 * (3.0 / 100.00).