Sub topic 5
Understand Parameter and Local Variable Scope
A variable’s scope is the part of a program where you can use its name. Scope matters because a variable declared in one part of a method may not be available in another.
A parameter is a variable named in a method’s declaration. An argument is the value supplied when the method is called. A local variable is a variable declared inside a method.
Follow the scope in a method
A block is a group of statements between braces, { and }. A method body is a block. An if statement can also contain a block inside the method body.
Save this program as ScopeDemo.java:
class ScopeDemo {
static int discountedPrice(int price, boolean member) {
int result = price;
if (member) {
int discount = 2;
result = price - discount;
System.out.println("Discount: " + discount);
}
// The discount variable is out of scope here.
return result;
}
public static void main(String[] args) {
int price = 10;
int finalPrice = discountedPrice(price, true);
System.out.println("Original: " + price);
System.out.println("Final: " + finalPrice);
}
}The static keyword lets this example call discountedPrice without creating an object. The boolean type holds either true or false.
In a terminal, a text window for running commands, compile and run the program:
javac ScopeDemo.java
java ScopeDemoA compiler is a program that checks Java code and translates it so it can run. The output is:
Discount: 2Original: 10Final: 8
Trace each variable
The call discountedPrice(price, true) gives the method the arguments 10 and true. Inside discountedPrice, the parameters price and member can be used throughout that method’s body.
The local variable result is declared near the start of the method. It can be used from its declaration to the end of the method’s block. The inner if block can use it too.
The local variable discount has a smaller scope. It is declared inside the if block, so it can be used only from its declaration to the end of that block. It cannot be used at the return statement.
The price in main and the price parameter in discountedPrice are different variables. They can have the same name because they belong to different method bodies. Neither method can directly use the other method’s local variables by name.
Watch declarations and braces
A local variable must have a value before you read it. In the example, result gets its first value when it is declared: int result = price;. It may get a new value inside the if block.
A variable also cannot be used before its declaration. When you get a compiler error about a name, check both where the variable is declared and where its block ends.
The same rule applies to a loop, which repeats statements. In for (int i = 0; i < 3; i++), the variable i is available in the loop header and body, but not after the loop ends.
Each call to a method has its own parameters and local variables. They do not keep their values for the next call. Also, you cannot declare another local variable with the same name inside a block where an existing local variable or parameter with that name is still in scope.
Exercise
If the call changes to discountedPrice(price, false), what value does the method return, and why?
Show answer
It returns 10. The if block does not run, so result keeps its starting value.
Exercise
Can the return statement use discount in the example? How could you make that name available there?
Show answer
No. discount is inside the if block. Declare and give it a value in the method block before the if statement instead.