This chapter reviews method parameters and local variables, as well as method overloading and method signature.
Method overloading means two or more methods have the same name but have different parameter lists: either a different number of parameters or different types of parameters. When a method is called, the corresponding method is invoked by matching the arguments in the call to the parameter lists of the methods. The name together with the number and types of a method's parameter list is called the signature of a method. The return type itself is not part of the signature of a method.
3. Formal and Actual Parameters
Answer:
- Will the instance variable
balance
hold its value permanently?- Yes —
balance
is part of the state of the object and holds its value as long as the object exists. - (Of course, an assignment statement might change value.)
- Will the parameter
amount
hold its value permanently?- No —
amount
is used only to pass in a value into the method. It does not permanently hold a value.
Formal and Actual Parameters
An identifier is a name used for a class, a variable, a method, or a parameter. The following definitions are useful:
- formal parameter — the identifier used in a method to stand for the value that is passed into the method by a caller.
- For example,
amount
is a formal parameter ofprocessDeposit
- For example,
- actual parameter — the actual value that is passed into the method by a caller.
- For example, the
200
used whenprocessDeposit
is called is an actual parameter. - actual parameters are often called arguments
- For example, the
When a method is called, the formal parameter is temporarily "bound" to the actual parameter. The method uses the formal parameter to stand for the actual value that the caller wants the method to use.
For example, the processDeposit
method uses the formal parameter amount
to stand for the actual value used in the procedure call:
balance = balance + amount ;
Formal parameters are bound to an actual value only as long as their method is active. When a method returns to its caller, the formal parameters no longer contain any values. They cannot be used to store the state of an object.
The next time a formal parameter is used, it starts with a fresh value. It does remember the previous value it had.
Question 3:
What is used to store the state of an object?