Parameters, Local Variables, and Overloading
Site: | Saylor Academy |
Course: | CS101: Introduction to Computer Science I |
Book: | Parameters, Local Variables, and Overloading |
Printed by: | Guest user |
Date: | Wednesday, 2 April 2025, 11:23 PM |
Description
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.
Table of contents
- 1. Parameters, Local Variables, and Overloading
- 2. Parameters
- 3. Formal and Actual Parameters
- 4. Parameters are Seen by their Own Method, Only
- 5. Scope of a Formal Parameter
- 6. What Statements See
- 7. One-way Glass
- 8. Assigning to a Parameter
- 9. Local Variables
- 10. Can't use the Same Name in the Same Scope
- 11. Instance Variable and Local Variable with Same Name
- 12. Review: for Loops
- 13. Mystery of the Many Scopes
- 14. Two Objects
- 15. Another Mystery
- 16. Method Overloading
- 17. Method Signature
- 18. Return Type is Not Part of the Signature
- 19. Overloaded println()
- 20. End of the Chapter
1. Parameters, Local Variables, and Overloading
The state of an object consists of the data it holds in its instance variables. Instance variables hold their values until they are explicitly changed or until the object is destroyed.
An object's methods frequently work with other values that are not held in instance variables. These values are held in local variables and parameters. This chapter discusses how these are declared and used.
Chapter Topics:
- Formal and Actual Parameters
- Scope of Parameters
- Local Variables
- Scope of Local Variables
- Method Overloading
- Signature of a Method
Question 1:
(Review:) What is a parameter of a method?
Source: Bradley Kjell, http://programmedlessons.org/Java9/chap51/ch51_01.html This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 License.
2. Parameters
Answer:
A parameter is a value that is sent to a method when it is called.
Parameters
Here is an example of a method that uses a parameter.
public class CheckingAccount { . . . . private int balance; . . . . public void processDeposit( int amount ) { balance = balance + amount ; } } |
amount
is used by a caller to send a value to the method. This is called passing a value into the method. Here is part of a main()
method that uses the parameter to
pass a value into the processDeposit()
method of an object:
public class CheckingAccountTester { public static void main( String[] args ) { CheckingAccount bobsAccount = new CheckingAccount( "999", "Bob", 100 ); bobsAccount.processDeposit( 200 ); . . . . . . } } |
When the statement
bobsAccount.processDeposit( 200 );
is executed, the parameter amount
is given the value 200. Now, as the processDeposit
method executes, that value is added to the object's instance variable balance
:
balance = balance + amount ;
Then the method finishes and control returns to main()
. The balance
of the bobsAccount
object has been changed.
Question 2:
- Will the instance variable
balance
of the object hold its value permanently? - Will the parameter
amount
of the object's method hold its value permanently?
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?
4. Parameters are Seen by their Own Method, Only
Answer:
Instance variables are used to store the state of an object. They hold values for as long as the object exists.
Parameters are Seen by their Own Method, Only
The formal parameters of a method can be seen only by the statements of their own method. This means that if a method tries to use a parameter of some other method, the compiler will find a syntax error.
Here is the CheckingAccount
class again, this time with a new definition of the toString()
method
public class CheckingAccount { private String accountNumber; private String accountHolder; private int balance; . . . . public void processDeposit( int amount ) { balance = balance + amount ; } // modified toString() method public String toString() { return "Account: " + accountNumber + "\tName: " + accountHolder + "\tBalance: " + amount ; } } |
Question 4:
Is this toString()
method correct?
5. Scope of a Formal Parameter
Answer:
No. The formal parameter amount
can only be used by the processDeposit
method. It cannot be used by any other method.
Scope of a Formal Parameter
public class CheckingAccount{ private String accountNumber; private String accountHolder; private int balance; . . . . public void processDeposit( int amount ) { // scope of amount starts here balance = balance + amount ; // scope of amount ends here } // modified toString() method public String toString() { System.out.println( balance + "\t" + amount ); // outside of the scope of amount } } |
The scope of a formal parameter is the section of source code that can see the parameter. The scope of a formal parameter is the body of its method. For example, the scope of amount
is the body of its method.
The toString()
method cannot see amount
because it is outside the scope of amount
. The compiler will not compile this modified program.
Question 5:
Can the toString()
method see the object's instance variables, such as balance
?
6. What Statements See
Answer:
Yes.
What Statements See
public class CheckingAccount { private String accountNumber; private String accountHolder; private int balance; . . . . public void processDeposit( int amount ) { // scope of amount starts here balance = balance + amount ; // scope of amount ends here } public void processCheck( int amount ) { // scope of amount starts here int charge; incrementUse(); if ( balance < 100000 ) charge = 15; else charge = 0; balance = balance - amount - charge ; // scope of amount ends here } } |
A method's statements can see the instance variables of their object. They cannot see the parameters and local variables of other methods. Look again at the CheckingAccount
class.
Two methods are using the same identifier, amount
, for two different formal parameters. Each method has its own formal parameter completely separate from the other method. This is OK. The scopes of the
two parameters do not overlap so the statements in one method cannot "see" the formal parameter of the other method.
For example the statement
balance = balance - amount - charge ;
from the second method can only see the formal parameter of that method. The scope of the formal parameter of the other method does not include this statement.
Of course, the formal parameters of any one parameter list must all have different names.
Question 6:
Could the two formal parameters (of the two methods) named amount
be of different
types? (For example, could one be an int
and the other a long
?)
7. One-way Glass
Answer:
Yes — since each can only be seen in the body of its method,
each can be declared to be of any type.
One-way Glass
One-way Glass

It is sometimes useful to visualize methods as being surrounded by a box of "one-way glass." A method can see local variables and parameters that are inside the box. A method can look out through the glass that surrounds it. But no outsider can see into the box.
The picture shows the one-way glass for the example program. The red lines show the box of one-way glass surrounding each method. The method can see out of its box (for example each method can see the instance variable balance
) but other methods can't see from the outside into the box of one-way glass.
In processDeposit()
the statement can see the variable balance
declared as a instance variable. It can also see the parameter amount
that is inside its box.
In processCheck()
statements can see the instance variable balance
, can see the parameter amount
, and can see the local variable charge
.
The method showCharge()
is defective because it contains a statement that attempts to look inside the box that surrounds processCheck()
.
The names of formal parameters (such as amount
) and local variables (such as charge
) are only visible from inside the glass box. However, outsiders can call the method and supply values for the formal parameters.
Question 7:
Is the name
of a method inside or outside the glass box?
8. Assigning to a Parameter
Answer:
Outside — so it can be called by other methods.
Assigning to a Parameter
class CheckingAccount { . . . . private int balance; public void processCheck( int amount ) { int charge; if ( balance < 100000 ) charge = 15; else charge = 0; balance = balance - amount - charge ; // change the local copy of the value in "amount" amount = 0 ; } } public class CheckingTester { public static void main ( String[] args ) { CheckingAccount act; int check = 5000; act = new CheckingAccount( "123-345-99", "Wanda Fish", 100000 ); System.out.println( "check:" + check ); // prints "5000" // call processCheck with a copy of the value 5000 act.processCheck( check ); System.out.println( "check:" + check ); // prints "5000" --- "check" was not changed } } |
A parameter is a "one-way message" that the caller uses to send values to a method.
Within the body of a method, a parameter is used just like any variable. It can be used in arithmetic expressions, in assignment statements, and so on.
However, changes made to the parameter do not have any effect outside the method body. A parameter is a local copy of whatever value the caller passed into the method. Any changes made to it affect only this local copy.
The formal parameter amount
is the name used for the value 5000 that processCheck()
has been sent. That method changes the value held in amount
, but this has no effect on any other variable.
When the method returns to its caller, the value in the parameter amount
and the value in the local variable charge
are forgotten.
Question 8:
Say that main()
called the method with an integer literal:
act.processCheck( 7000 ); // call processCheck with the value 7000
Is this OK?
9. Local Variables
Answer:
Is this OK?
Sure. The caller has sent the value 7000 to the method. Inside the method, that value is held in the parameter amount
. At the end of the method, the statement
amount = 0;
puts a zero into the parameter, but does not affect anything in main()
Local Variables
public class CheckingAccount { . . . . private int balance; public void processCheck( int amount ) { int charge; // scope of charge starts here incrementUse(); if ( balance < 100000 ) charge = 15; else charge = 0; balance = balance - amount - charge ; // scope of charge ends here } } |
A local variable is a variable that is declared inside of the body of a method.
The scope of a local variable starts from where it is declared and ends at the end of the block that it is in. Recall that a block is a group of statements inside of braces, {}
.
For example, charge
of processCheck
is a local variable:
The local variable charge
is used to hold a temporary value while something is being calculated. Local variables are not used to hold the permanent values of an object's state. They have a value only during the brief amount of time that a method is active.
Question 9:
Is the scope of a local variable always the entire body of a method?
10. Can't use the Same Name in the Same Scope
Answer:
No — the scope starts where the variable was declared and continues to the end of its block.
Sometimes a local variable is declared in the middle of a block, close to the
statement which first uses it.
Often, local variables are declared at the beginning of a block so that their scope is the
entire block. But this is not a requirement of syntax.
Can't use the Same Name in the Same Scope
class CheckingAccount { . . . . private int balance; public void processCheck( int amount ) { int amount; incrementUse(); if ( balance < 100000 ) amount = 15; // which amount ??? else amount = 0; // which amount ??? balance = balance - amount ; // which amount ??? } } |
Don't use the same identifier twice in the same scope. The example shows this mistake.
The scope of the formal parameter amount
overlaps the scope of the local variable (also named amount
), so this is a mistake. (In terms of "one-way glass", both identifiers are inside the box.)
This is a different situation than a previous example where two methods used the same name for their formal parameters. In that situation the scopes did not overlap and there was no confusion. (In terms of "one-way glass", each identifier was inside its own box.)
Question 10:
Can the same identifier be used as a name for a local variable in two different methods?
11. Instance Variable and Local Variable with Same Name
Answer:
Yes — the scopes will not overlap so there will be two local variables, one per method,
each with the same name.
Instance Variable and Local Variable
with Same Name
with Same Name
Although it is usually a bad idea, you can declare a formal parameter or a local variable with the same name as one of the instance variables.
class CheckingAccount { . . . . private int balance; . . . . public void processDeposit( int amount ) { int balance = 0; // New declaration of balance. balance = balance + amount ; // This uses the local variable, balance. } } |
This is not a syntax error (although it will probably result in a logic error, a bug). The compiler will compile this code without complaint. The second declaration of balance
(the one in red) creates a local variable
for the processDeposit
method. The scope of this variable starts with its declaration and ends at the end of the block (as with all local variables). So the next statement uses the local variable, not the instance variable.
The local variable balance
is said to shadow the instance variable.
When this modified method is called, it will add amount
to the local variable balance
, and then return to the caller. The local variable will no longer hold a value after the method has returned. The
instance variable will not have been changed.
Hint: Think of statements as looking "upward" from their own location inside their "glass box" to find a variable. If they find a variable inside their box (scope), that is the one they use. An instance variable of the same name will have been shadowed.
They can look outside of their "glass box" in any direction if they fail to find a variable inside their own method.
You need to be careful if you use the same name for an instance variable and for a local variable. But it is not a syntax error, so the compiler will not warn you of impending doom.
Question 11:
Examine this modification:
public class CheckingAccount
{
. . . .
private int balance;
. . . .
public void processDeposit( int amount )
{
int balance = 0; // New declaration of balance.
this.balance = balance + amount ; // ??????
}
}
Does the method change the instance variable balance ?
12. Review: for Loops
Answer:
Yes. The variable that follows the reserved word this
is an instance variable (part of "this" object).
Review: for
Loops
for
Loops
Here is a for
loop:
{ int sum = 0; for ( int count = 0; count < 10; count++ ) { System.out.print( count + " " ); sum = sum+count; } }
Scope Rule: a variable declared in a for
statement can only be used in that statement and the body of the loop. The scope of count
is only the loop body.
The following is NOT correct:
{ int sum = 0; for ( int count = 0; count < 10; count++ ) { System.out.print( count + " " ); sum = sum+count; } // NOT part of the loop body System.out.println( "\nAfter the loop count is: " + count ); }
Since the println()
statement is not part of the loop body, count
cannot be used here. The compiler will complain that it "cannot find the symbol: count" when it sees this count
.
This can be a baffling error message if you forget the scope rule.
However, sum
is declared outside of the for
loop, so its scope starts with its declaration and continued to the end of the block.
Question 12:
Is the following code fragment correct?
int sum = 0;
for ( int j = 0; j < 8; j++ )
sum = sum + j;
System.out.println( "The sum is: " + sum );
13. Mystery of the Many Scopes
Answer:
int sum = 0;
for ( int j = 0; j < 8; j++ )
sum = sum + j;
System.out.println( "The sum is: " + sum );
Yes. Here the println()
statement is within the scope of sum
.
Mystery of the Many Scopes
Examine the following program. What does it print?
class Mystery
{
private int sum;
public Mystery( int sum )
{
this.sum = sum;
}
public void increment( int inc )
{
sum = sum + inc;
System.out.println("Mystery sum: " + sum );
}
}
public class Tester
{
public static void main ( String[] args)
{
int sum = 99;
Mystery myst = new Mystery( 34 );
myst.increment( 6 );
System.out.println("sum: " + sum );
}
}
Notice that the identifier sum
is used for three different things:
- The instance variable of a
Mystery
object. - The parameter of the
Mystery
constructor. - The local variable of the
main()
method.
Question 13:
What is printed?
14. Two Objects
Answer:
Mystery sum: 40
sum: 99
Two Objects
Colors in the following show how this works. this
is used in the constructor where the instance variable, not the parameter, should be used.
class Mystery
{
private int sum;
public Mystery( int sum )
{
this.sum = sum;
}
public void increment( int inc )
{
sum = sum + inc;
System.out.println("Mystery sum: " + sum );
}
}
public class Tester
{
public static void main ( String[] args)
{
int sum = 99;
Mystery myst = new Mystery( 34 );
myst.increment( 6 );
System.out.println("sum: " + sum );
}
}
Now look at this modified version:
class Mystery
{
private int sum;
public Mystery( int x )
{
sum = x;
}
public void increment( int inc )
{
sum = sum + inc;
System.out.println("Mystery sum: " + sum );
}
}
public class Tester
{
public static void main ( String[] args)
{
Mystery mystA = new Mystery( 34 );
Mystery mystB = new Mystery( 13 );
mystA.increment( 6 );
mystB.increment( 7 );
}
}
Question 14:
Now what is printed?
15. Another Mystery
Answer:
Mystery sum: 40
Mystery sum: 20
Each object has its own instance variables, of course.
Another Mystery
Yet another mystery:
class Mystery
{
private int sum;
public Mystery( int x )
{
sum = x;
}
public void increment( int inc )
{
sum = sum + inc;
}
public void increase( int sum )
{
sum++ ;
}
public String toString()
{
return ("sum: " + sum );
}
}
public class Tester
{
public static void main ( String[] args)
{
Mystery mystA = new Mystery( 10 );
Mystery mystB = new Mystery( 20 );
mystA.increment( 5 );
mystB.increase( 3 );
System.out.println("mystA " + mystA + " mystB " + mystB);
}
}
Question 15:
Now what is printed? Beware: this is a trick question.
16. Method Overloading
Answer:
mystA sum: 15 mystB sum: 20
The instance variable in mystB
did not change.
The trick: the parameter sum
of the second method shadowed the instance varaible.
Method Overloading
Overloading is when two or more methods of a class have the same name but have different parameter lists. When a method is called, the correct method is picked by matching the actual parameters in the call to the formal parameter lists of the methods.
Review: another use of the term "overloading" is when an operator calls for different operations depending on its operands. For example +
can mean integer addition, floating point addition, or string concatenation depending on its
operands.
Say that two processDeposit()
methods were needed:
- One for ordinary deposits, for which there is no service charge.
- One for other deposits, for which there is a service charge.
public class CheckingAccount { . . . . private int balance; . . . . public void processDeposit( int amount ) { balance = balance + amount ; } public void processDeposit( int amount, int serviceCharge ) { balance = balance + amount - serviceCharge; } } |
The above code implements these requirements. Here is an examplemain()
method that uses both methods:
class CheckingAccountTester { public static void main( String[] args ) { CheckingAccount bobsAccount = new CheckingAccount( "999", "Bob", 100 ); bobsAccount.processDeposit( 200 ); // statement A bobsAccount.processDeposit( 200, 25 ); // statement B } } |
Question 16:
Examine main()
.
Which method, processDeposit(int)
or processDeposit(int, int)
does each statement call?
- statement A calls
- statement B calls
17. Method Signature
Answer:
- statement A calls
processDeposit(int)
- statement B calls
processDeposit(int, int)
Method Signature
When several methods have the same name, only one is picked by a method call. The types of the actual parameters in a method call are matched with the types of the formal parameters of the methods. If an exact match cannot be made, then the actual parameters are converted to types that match the formal parameters if this can be done without potential loss of information.
For example, the call
bobsAccount.processDeposit( 200, 25 ); //statement A
matches this method declaration:
public void
processDeposit( int amount, int serviceCharge )
because the number and types of the actual parameters matches the number and types of the formal parameters.
The signature of a method is:
- Its name
- The number and types of its parameters, in order
The signatures of the methods in a class must be unique. For example, the signatures of the two processDeposit
methods are:
- processDeposit( int )
- processDeposit( int, int )
The names of the parameters are not part of the signature because parameter names are not visible outside of their scope.
The return type is not part of the signature. The visibility modifier is not part of the signature.
Question 17:
Say that a class has the following two methods:
float chargePenalty( int amount ) { ... }
int chargePenalty( int penalty ) { ... }
Do these methods have unique signatures?
18. Return Type is Not Part of the Signature
Answer:
No.
The names of the formal parameters are not part of the signature, nor is the return type. The signatures of the two methods are:
chargePenalty( int )
chargePenalty( int )
Both methods have the same signature.
Return Type is Not Part of the Signature
It might seem strange that the return type is not part of the signature. But a potentially confusing situation is avoided by keeping the return type out of the signature. Say that there were two methods that differ only in their return type:
float chargePenalty( int amount ) { ... } int chargePenalty( int penalty ) { ... }
and that main used one of the methods:
class CheckingAccountTester
{
public static void main( String[] args )
{
CheckingAccount bobsAccount = new CheckingAccount( "999", "Bob", 100 );
double result = bobsAccount.chargePenalty( 60 )
;
}
}
Which method should be called?
Either method matches the statement, since both have and int
parameter and both return a type that can be converted to double
. (Both int
and float
can be converted
to double
.)
To avoid confusion in situations like this, the return type is not counted as part of the signature.
Question 18:
Say that a class has these two methods:
public void changeInterestRate( double newRate ) { ... }
public void changeInterestRate( int newRate ) { ... }
Do these methods have unique signatures?
19. Overloaded println()
Answer:
Yes. The names of the formal parameters do not have to be unique.
Overloaded println()
println()
We have already been using overloading. The println()
method (for example) has several overloaded versions:
- println( )
- println( boolean x )
- println( char x )
- println( double x )
- println( int x )
- println( String x )
- ... others
Which version of println()
gets called depends on the argument.
Question 19:
Which version of println()
is called in the following:
System.out.println( "The result is: " + 34 );
20. End of the Chapter
Answer:
println( String x )
String concatenation is performed before println()
is called, resulting in an argument that is a String
reference.
End of the Chapter
End of the Chapter
You have reached the end this chapter. You may wish to review the following, unless you feel overloaded. Click on a subject that interests you to go to where it was discussed.
- parameter passing Passing a value to a method.
- parameter, formal and actual Formal and Actual parameters.
- scope, of formal parameter The scope of a formal parameter.
- visibility of variables What the statements of a method can "see."
- parameter, assignment to Assignment of values to a parameter.
- local variable Local variables.
- scope, of local variable The scope of a local variable.
- overloading, of methods Overloading.
- signature Signature of a method.