Scope
Objective #1: Explain scope with regard to local variables and instance fields.
- The scope of a variable is the area of
a program in which the variable can be used without causing an error.
- A variable can only be used within
the method where it is declared. The scope of a variable that is declared within a method is called local. Such variables are called local variables.
In the example below, the local variable named area cannot be used anywhere else in the class other than the computeArea method since it is being declared as a double in this method:
public double computeArea()
{
double area = 0.0;
area = myWidth * myWidth;
return area;
}
- The
scope of an instance variable (i.e. a property) is the class that it's contained
in. The instance variable can be used in any method in the class. The scope of an instance variable is wider than a local variable. The scope is considered to be the whole class.
In the example below, the instance variable myWidth is legally used in both methods computeArea and computePerimeter
public class Square
{
private double myWidth;
public double computeArea()
{
return myWidth * myWidth;
}
public double computePerimeter()
{
return myWidth * 4;
}
. . . . .
- A variable that is declared inside a for loop such as i in the example below
for (int i = 0; i < 10; i++)
can only be used in that particular loop.
An error occurs in the following example
int sum = 0;
for (int i = 0; i < 10; i++)
{
sum = sum + i;
}
System.out.println(sum / i); // error occurs because i is out of scope
In the following version no error occurs since i is declared outside of the loop
int sum = 0;
int i = 0;
for (i = 0; i < 10; i++)
{
sum = sum + i;
}
System.out.println(sum / i);
- You will receive a compiler error if you have two variables with the same
name that have overlapping scope as in this example:
public static void doNothing()
{
int i = 50;
for (int i = 0; i < 10; i++)
{
System.out.println(i);
}
}
- A local variable must be initialized to a value when it
is declared within a method or a compile error will result.
A compile error occurs in the following method because area is declared but not initialized to some value such as zero.
public double computeArea()
{
double area; // should be double area = 0;
area = myWidth * myWidth;
return area;
}
- A class developer usually initializes int and double instance variables
in the constructos of a class. However, the compiler will automatically initialize int and double instance variables
to the value zero even if you do not initialize them in a constructor.
- Usually an instance variable is declared as
private so it cannot be directly used in a client program.
- Shadowing results when a local variable in a method
is given the same name as an instance variable in the class. Shadowing is bad style and should be avoided even though it doesn't cause a compile error.
public class Bug
{
public Bug()
{
int myWeight = 10;
System.out.println(myWeight);
// 10 is displayed since the local variable is initialized to 10
System.out.println(this.myWeight);
// 0 is displayed since the instance variable myWeight is automatically initialized to zero
}
. . .
private int myWeight;
}
Objective #2: Understand how to swap two variables.
- Unfortunately, it is more difficult to swap two variables
using a method in Java than it is in C++.
The following Java call statement and swap method would compile fine but it would not cause the values in calling method local variables a and b to be exchanged
call statement: swap(a, b);
public static void swap(int num1, int num2)
{
int temp = 0;
temp = num1;
num1 = num2;
num2 = temp;
}
since the changes to the formal parameters num1 and num2 in
the called method would not apply to the corresponding actual parameters a and b in
the calling method.
However, assume that there is a class named Pair that has two int properties. Then a Pair object could be passed to the following version of swap
call statement: swap(myPair);
public static void swap(Pair thePair)
{
int temp = thePair.getNum1();
thePair.setNum1(thePair.getNum2());
thePair.setNum2(temp);
}
since the properties of an object may be changed in a method even if the object itself cannot be overwritten.
You can also use an array to swap two values in Java. Here is a method that uses an array as a parameter to a Java swap method
public static void swap(int[] theArray)
{
int temp = theArray[0];
theArray[0] = theArray[1];
theArray[1] = temp;
}
- This information is probably not covered by the AP exam but it is useful to understand.
In Java, every parameter is passed by value. This means that a copy of the value is passed to the method and not the variable or the memory address of the variable itself. When an int value is passed to a method, the value is passed. When a primitive variable such as an int variable is passed, a copy of the value that is stored in the int variable is passed. The memory address of where the int variable is stored in the memory of the computer is not passed. If the called method changes the value stored in the passed parameter, it will have no effect on the original passed parameter in the calling method. When an object variable is passed to a method, a copy of the object variable's object reference (i.e. a copy of the memory address of where the object variable is stored) is passed to the method. If the called method tries to change the object reference it will not have any affect on the original object variable in the calling method. There are no pointer variables in Java like there are in C and C++. The developers of Java wanted to avoid pointer variables which have a reputation for allowing poor C and C++ programmers to write buggy programs. You cannot pass by reference using the & like you can in C and C++ or with ByRef in Visual Basic.