Local Variables
A local variable is a variable that is declared within a
method declaration.
Local variables are accessible only from the method
in which they are declared.
Memory space for local variables are allocated only
during the execution of the method. When the
method execution completes, memory space will be
deallocated.
Grukkee … out to reality LocalVariables.java
Parameters
The formal parameters defined in the method header
are also local variables
Formal parameters receive copies of the actual
parameters passed in the call
The values are copied in order, 1st to 1st, 2nd to 2nd,
etc
Ooodles … out to reality Parameters.java
Sample Method
private static double fromDollar(double dollar)
{
double
amount, fee;
Local
Variables
fee
= exchangeRate - feeRate;
amount
= dollar * fee;
return amount;
}
Parameter
Memory Allocation for Local Variables - 1
Code
A
finalAmount = fromDollar(200);
private static double fromDollar(
double dollar) {
double amount, rate;
rate
= EXCHANGE_RATE - FEE_RATE;
amount = dollar * rate;
return(amount);
}
At A before fromDollar
A. Local variables do
not exist before the
method execution
State of
Memory
Memory Allocation for Local Variables - 2
Code
private static double fromDollar(
double dollar) {
double amount, rate;
rate
= EXCHANGE_RATE - FEE_RATE;
amount = dollar * rate;
return(amount);
}
B
finalAmount = fromDollar(200);
After B
is executed
dollar
amount
State of
Memory
rate
200.0
B. Memory space is
allocated for the local
variables and parameter.
Memory Allocation for Local Variables - 3
Code
private static double fromDollar(
double dollar) {
double amount, rate;
rate
= EXCHANGE_RATE - FEE_RATE;
amount = dollar * rate;
return(amount);
}
finalAmount = fromDollar(200);
C
After C
is executed
dollar
State of
Memory
200.0
amount
24846.3
rate
124.2315
C. Computed values
are assigned to the local
variables.
Memory Allocation for Local Variables - 4
Code
finalAmount = fromDollar(200);
D
private static double fromDollar(
double dollar) {
double amount, rate;
rate
= EXCHANGE_RATE - FEE_RATE;
amount = dollar * rate;
return(amount);
}
At D after fromDollar
D. Memory space is
deallocated upon exiting
the fromDollar method.
State of
Memory
Pass-By-Value Scheme - 1
Code
A
x = 10;
y = 20;
myMethod( x, y );
private static void myMethod(int one,
float two) {
one = 25;
two = 35.4f;
}
At A before myMethod
x
State of
Memory
y
10
A. Local variables do
20
10
not exist before the
method execution
Pass-By-Value Scheme - 2
Code
x = 10;
y = 20;
myMethod( x, y );
private static void myMethod(int one,
float two) {
one = 25;
two = 35.4f;
}
Values are copied at
x
State of
Memory
y
10
20
10
one
two
B
B
10
B. The values of
20.0f
10
arguments are copied
to the parameters.
Pass-By-Value Scheme - 3
Code
x = 10;
y = 20;
myMethod( x, y );
private static void myMethod(int one,
float two) {
one = 25;
two = 35.4f;
}
C
After C
x
State of
Memory
y
10
20
10
is executed
one
two
25
10
C. The values of
35.4f
10
parameters are
changed.
Pass-By-Value Scheme - 4
Code
x = 10;
y = 20;
myMethod( x, y );
private static void myMethod(int one,
float two) {
one = 25;
two = 35.4f;
}
D
At D after myMethod
x
State of
Memory
y
10
D. Parameters are
20
10
erased. Arguments
remain unchanged.
Actual Parameters
Formal and actual parameters need not (but can)
have the same name.
Changes to the formal parameters have no effect in
the calling code because they are local!
Actual parameters need not be variables.
Ack … out to reality NonVariableParameters.java
Arguments & Parameters: Points to Remember
1. Arguments are passed to a method using the pass-by-value
scheme.
2. Arguments are matched to the parameters from left to right.
The data type of an argument must be assignment compatible
to the data type of the matching parameter.
3. The number of arguments in the method call must match the
number of parameters in the method definition.
4. Parameters and arguments do not have to have the same
name.
5. Local copies, which are distinct from arguments, are created
even if the parameters and arguments share the same name.
6. Parameters are input to a method, and they are local to the
method. Changes made to the parameters will not affect the
value of corresponding arguments.
© Copyright 2026 Paperzz