Variable scope
Lesson content
- Variable visibility (variable scope)
- Visibility conflict and reserved word this
- Most common errors
Variable visibility (variable scope)
As a reminder, depending on where they are declared in the code, there are four types of variables:
- Local variables (or simply variables) – variables declared within the body of a method.
- Attributes – variables directly belonging to the objects of a class. They are defined within the body of the class.
- Global variables – variables not belonging to individual objects of a class, but used throughout the program. They are defined similarly to attributes.
- Parameters – variables of a method declared in the method’s signature (representing input values for the method). Technically, they are also local (method) variables.
To fully understand the functioning of methods, it is necessary to introduce and explain the concept of variable visibility. Variable visibility determines from which part of the program a variable can be accessed. In literature, instead of the term “variable visibility” the term variable scope is often used.
The rules defining variable scope in the Java programming language are related to variable types:
- All local variables (including parameters) are “visible” (can be accessed) only within the body of that method and nowhere else.
- All attributes of a class are always visible within the body of that class and can be accessed outside the class if not restricted (see lesson on access levels).
- Global variables can be accessed within the class body and more broadly, unless restricted (see lesson on access levels).
Example 1
Let’s say the class Calculator has:
- An attribute pi representing the corresponding mathematical constant (the value is roughly 3.14).
- A method add that takes two integers as parameters and returns the result of their addition.
- A method calculateCircleCircumference that takes the radius of a circle as a parameter and returns the circumference as the result (the formula is 2 * radius * pi).
1 class Calculator {
2 double pi = 3.14;
3
4 int add(int a, int b){
5 int result;
6 result = a + b;
7 return result;
8
9 //Or shorter, all in one command
10 //return a + b;
11 }
12
13 double calculateCircleCircumference(double radius){
14 //Not allowed since a and b are local
15 //variables visible only in the add method
16 //System.out.println(a + b);
17
18 double result;
19 result = 2 * radius * pi;
20 return result;
21
22 //Or shorter, all in one command
23 //return 2 * radius * pi;
24 }
25 }
Attribute pi is an attribute of the class Calculator, so it is visible (it can be accessed and used) within the body of any method of the Calculator class (it is used in the method body of calculateCircleCircumference).
On the other hand, variables a and b are parameters of the method add. This means they are visible (can be used and accessed) only within the body of this method. If an attempt is made to access variables a and b from the body of the method calculateCircleCircumference, a syntax error will occur.
The same applies to the parameter radius – it is visible only within the body of the calculateCircleCircumference method and nowhere else.
A quick review of the code might suggest there is a conflict because two variables named result are declared — one in the body of the first method, and the other in the body of the second method. However, this is not the case. In Java, these two variables are considered completely different because the result variable in the add method is only visible within the body of the add method, while the result variable in the calculateCircleCircumference method is only visible within the body of the calculateCircleCircumference method.
Visibility conflict and reserved word this
A conflict in “visibility” between parameters and attributes can arise if a method’s parameter or variable has the same name as an attribute of the class. In such cases, the reserved word this is used to distinguish the attribute from other variables. This reserved word is essentially a reference (pointer) to the object itself.
Example 2
Let’s say the class Person has:
- An attribute name
- A method setName that takes a name as a parameter and assigns this value to the attribute. The parameter is also called name.
1 class Person {
2 String name;
3
4 void setName(String name){
5 //Incorrect
6 name = name;
7
8 //Correct, use this to distinguish between
9 //attribute and local variable
10 //this.name = name;
11 }
12 }
What can first be noticed is that the assignment statement to the attribute name is very unclear. Which one is the attribute, and which is the parameter? Both are called “name”. Therefore, what is the effect of the command:
1 name = name;
The answer is that Java will interpret both sides as the parameter, so the assignment to the attribute name will not happen. To make the method do what is needed, the reserved word this is used to refer to the attribute. After the correction, the method should look like this:
1 void setName(String name) {
2 this.name = name;
3 }
The expression on the left-hand side of the equality sign refers to the attribute name (this.name), while the expression on the right-hand side refers to the parameter name.
Most common errors
Some of the most common syntax errors related to variable visibility are:
- Attempting to access a local variable or parameter from a method where it is not declared (from another method or class):
1 class X {
2
3 void methodA(int parameterA){
4 System.out.println(parameterA);
5 }
6
7 void methodB(){
8 System.out.println(parameterA);
9 }
10 }
instead of (correct):
1 class X {
2
3 void methodA(int parameterA){
4 System.out.println(parameterA);
5 }
6
7 void methodB(){
8 System.out.println("Some message");
9 }
10 }
Other common mistakes that are not syntax errors but affect the program’s behavior include:
- Incorrectly accessing a local variable instead of an attribute — if both the attribute and the variable have the same name, and the reserved word this is not used, for example:
1 class Car {
2 String owner;
3
4 void setOwner(String owner) {
5 owner = owner;
6 }
7 }
instead of (correct):
1 class Car {
2 String owner;
3
4 void setOwner(String owner) {
5 this.owner = owner;
6 }
7 }