The scope of a variable specifies the region of the source program where that variable is known, accessible and can be used. In Java, the declared variable has a definite scope. When a variable is defined within a class, its scope determines whether it can be used only within the defined class or outside of the class also.
Local variables can be used only within the block in which they are defined. The scope of instance variables covers the entire class, so they can be used by any of the methods within that class.
Variables must be declared before usage within a scope. Variables that are declared in an outer scope can also be used in an inner scope. The unique way in which Java checks for the scope of a given variable makes it possible to create a variable in an inner scope such that a definition of that variable hides its original value. Program makes this point clear.
Checking the scope of a variable.
class ScopeOfVariables { static int var = 10; ScopeOfVariables() { // variable var from outer scope is accessed here System.out.println(''Variable accessed from outer scope =" +var); } public static void main(String args[]) { int var 25; //variable var from current scope is accessed here ScopeOfVariables sc= new ScopeOfVariables(); System.out.println("Variable accessed from current scope =" +var); } }
The output of Program is given below:
Variable accessed from outer scope = 10
Variable accessed from current scope = 25
The ScopeOfVariables class has two variables defined with the same name (var). One variable (local variable) is defined within the main method, which is initialized to 25, and another variable (instance variable) is declared within the class, which is initialized to 10. When an instance of the class ScopeOfVariables is created, its constructor is invoked. As variable var is accessed inside the constructor, it will check for declaration of the variable in the current scope (that is, within braces). As the declaration is not found in its current scope, it goes to upper level and checks the variable declaration. Since it is defined in the class (outside the method), now, the variable takes the value 10 and the program displays the first line of the output. On the other hand, in the main method, the local variable var is accessed in its current scope. Here, the local variable hides the instance variable. So, this variable takes the value 25 and the println() method will print the second line of the output.
In order to avoid confusion it is better to give different names to local variables and instance variables.
Another way to get around this particular problem is to use the keyword this. The instance variable can be referred to by using this.var in the above program. By referring explicitly to the instance variable using its object, scope conflict can be avoided.
Other definitions that can cause bugs
Another situation in variable naming is when a variable that occurs in a super-class is redefined in the sub-class. This can create subtle bugs in the program. This would lead methods that were intended to change the value of an instance variable to change the wrong ones.
Another bug might occur when an object is cast from one class to another. In such cases, the value of instance variable is likely to change because it is accessing the value from the super-class instead of the child class.
It is recommended that before defining variables in a sub-class, the programmers take note of the variables in each of the super classes corresponding to that class and avoid such problems. This would help in preventing duplication of variables which can lead to bugs.