Variable Scope
There are rules about when we can use certain variables in our programs. If we declare a variable inside a set of brackets:
{
//variable declaration
}Then we can use that variable anywhere (after its declaration) inside those brackets. However, we cannot use the variable outside of the brackets (either before them or after them). These brackets can belong to a class, the main method, an if statement, or a loop – the rules are always the same.
Variable scope example
For example, if we do:
Scanner s = new Scanner(System.in);
int sum = 0;
int count = 0;
while (count < 100)
{
System.out.print("Enter a number: ");
int num = s.nextInt();
sum = sum + num;
count = count + 1;
}
//illegal – num is not visible here
System.out.printf("The last number was: %d%n", num);
System.out.printf("The sum is: %d%d", sum);Then we will get a compiler error. The num variable was declared inside the while loop
brackets, and we cannot see it once the while loop has ended. If we did want to be able to print
out the last number the user entered, we would have to declare the num variable before the while
loop:
Scanner s = new Scanner(System.in);
//now num is visible everywhere in this code fragment
int num = 0;
int sum = 0;
int count = 0;
while (count < 100)
{
System.out.print("Enter a number: ");
int num = s.nextInt();
sum = sum + num;
count = count + 1;
}
System.out.printf("The last number was: %d%n", num);
System.out.printf("The sum is: %d%d", sum);Additionally, declaring the loop variable in a for loop has the same rules as declaring it inside the loop. For example:
for (int i = 0; i < 10; i++)
{
//do something
}The i variable is only visible inside the for loop, just as if it was declared at the beginning of
the loop.
These requirements are called scope rules. If we are outside the brackets where a variable was
declared, then we are outside the scope of that variable.
Redeclaring variables
Because we can’t always see variables that we have declared, we can reuse variable names. For example:
int count = 0;
while (count < 10)
{
int num;
//do something with num
count++;
}
count = 0;
while (count < 10)
{
int num;
//do something with num
count++;
}Here, we have reused the num variable. The first declaration of num is only visible to the first
while loop. If we want to use the name num again in the second while loop, we must redeclare it
(because we can’t see the other variable). It is perfectly fine (and encouraged) to reuse variable
names in this way.
We can also redeclare variables when we CAN still see the original variable, but it has a different result. For example:
int num = 10;
int count = 0;
while (count < 5)
{
int num = 2;
count = count + num;
}
System.out.println(num);Here, we declared num outside the while loop. Thus the num variable would have been visible
inside the while loop as well. However, we declared num AGAIN inside the loop. Now there’s
a conflict – we can see the variable outside the loop and the new variable inside the loop. In this
case, the compiler always assumes you mean the inner-most variable declaration. So in the
while loop, when you add num to your count, it assumes you mean the inner-most num – the
one declared inside the loop. So each time in the loop, 2 is added to your count.
Once you are outside the loop, the while-loop num is no longer visible. So the print statement
prints out the original num value – 10.
This type of variable declaration is confusing and not recommended. In general, if a variable
with some name is visible where you are, don’t reuse the name for a different variable.