Scope refers to the visibility of the variables. In other words, scope means in which parts of the program you can see and use your variable. In Java, broadly there are two types of scope:
- Class scope
- Method scope
We will defer discussing class scope to after we have discussed classes. Right now, we will look into method scope.
Method Scope
The variables that are declared within a method are called as local variables. Generally, local variables are accessible within the set of braces they are declared in. Earlier in the course, in the Code Blocks and Tokens lecture, we discussed that opening and closing curly braces define a block of code. The scope of a local variable is the code block within which it is declared. The variable is not accessible outside the block. Let’s see this in a BlueJ program:
public class ScopeDemo
{
public void demoMethodScope() {
int localVariable1 = 10;
if (localVariable1 == 10) {
float f1 = 25.5f;
System.out.println("Value of f1 is " + f1);
}
/*
* Uncommenting the below line
* will result in compile time
* error as f1 is not accessible
* outside the if block
*/
//System.out.println(f1);
}
}
The opening and closing curly braces of demoMethodScope
define its scope. I declare an int variable localVariable1
and initialize it with a value of 10
. As localVariable1
is defined within the scope of demoMethodScope
method, it is accessible throughout this method.
Next, I add an if
statement to create another scope. In this code block, I declare a float
variable f1
and initialize it with a value of 25.5f
. After that, I print its value. This float variable f1
is only accessible within the scope of the if
block. Outside the curly braces defining the scope of the if
block, f1
is not accessible. If I uncomment the line //System.out.println(f1);
in the above program, I will get an error because f1
is not accessible outside the if
block where it is defined. Let me show it to you:
Nested Scope
Code blocks can be nested meaning I can declare a block inside another block. This leads to nested scope. In case of nested scope, variables declared in the outer scope are accessible to the code in inner scope. But the variables that are declared in the inner scope are not accessible outside that block. It is time to look at a BlueJ program to better understand the concept of nested scope.
public class NestedScopeDemo
{
public void nestedDemo() {
//outerScopeVar is accessible through out nestedDemo method
int outerScopeVar = 100;
if (outerScopeVar == 100) { //start a nested scope within nestedDemo method block
//innerScopeVar is known only inside this if block
int innerScopeVar = 500;
//Both outerScopeVar & innerScopeVar are accessible inside this block
System.out.println("Inside if block::innerScopeVar = " + innerScopeVar);
System.out.println("Inside if block::outerScopeVar = " + outerScopeVar);
}
/*
* Next line will result in an error
* as innerScopeVar is not accessible here
* It is only accessible within the if block
* inside which it is declared
*/
// innerScopeVar = 1000;
//outerScopeVar is accessible here
System.out.println("Inside demoNestedScope block::outerScopeVar = " + outerScopeVar);
}
}
The opening and closing curly braces of nestedDemo
method define its scope. The local variable outerScopeVar
can be seen and used throughout the nestedDemo
method. Next, the if
block defines another scope inside the outer scope of nestedDemo
method. We declare another local variable innerScopeVar
inside the if
block. Both the variables innerScopeVar
and outerScopeVar
are accessible inside this inner scope. outerScopeVar
which is declared outside this scope is visible here because variables defined in the outer scope are accessible within inner scope.
If I uncomment the line — // innerScopeVar = 1000;
, it will give an error as innerScopeVar
is not known here. Variables declared in the inner scope are only accessible within that block. They are not accessible in the outer scope. outerScopeVar
is still accessible as it is declared inside the nestedDemo method scope.
One important thing to remember here is that inside the inner scope, you cannot declare a variable having the same name as the one in the outer scope.
Here I am in the inner scope of if
block. If I try to declare a variable with the name outerScopeVar
, it will lead to an error. The error says — variable outerScopeVar is already defined in method nestedDemo
.
Variables can be declared at any point within a block, but they are only valid after they have been declared. But if I try to use a variable before declaring it, the compiler will give an error.
Lifetime of Variables
Variables are created when their scope is entered and destroyed when their scope is left. The lifetime of a variable is confined to its scope.
If a variable declaration includes an initializer, then that variable will be reinitialized each time the block in which it is declared is entered.
Time to look at an example BlueJ program to understand lifetime of variables:
public class LifetimeOfVariable
{
public void demoLifetime() {
int index = -1;
if (index == -1) {
float foo = 64.76f; //foo is created when this line gets executed
System.out.println("Value of foo is " + foo);
} //foo is destroyed here
System.out.println("");
for (index = 0; index < 5; index++) {
int bar = 10;
System.out.println("Value of bar is " + bar);
bar = 20;
System.out.println("New value of bar is " + bar);
}
}
}
We declare and initialize a variable foo
inside the scope of if
block. The variable foo
gets created when this line float foo = 64.76f;
is executed and when the program control leaves the scope of if
block foo
is destroyed. After this point, foo
doesn’t contain the value of 64.76
.
Let’s execute the program now and then move to the for block. Below is the output of the program:
Inside the for
loop, we declare and initialize a variable bar
. Then we change its value to 20
. At the start of every iteration of the for
loop, program control enters this code block. The variable bar
is created and initialized with the value 10
. Next, the value of bar
is changed to 20
. At the end of iteration, the code block is exited. The variable bar
is destroyed, and it no longer holds the value 20
. That’s why you see the values 10
and 20
printed in each iteration of the for
loop.