next up previous contents
Next: Passing arrays to methods Up: 6 Multiple methods Previous: Method naming   Contents

Class variables, method variables and scope

Look again at the example that was used earlier, reproduced below. There are two variables x and y.

/**
 * Purpose: Example of program with two methods where one method
 *          takes an argument.  The program prints the value of
 *          x squared to the screen.
 */
public class ArgExample
{
    // method to print square of a value to screen
    public static void printSquare(double y)
    {
        System.out.println(y*y);
        return;
    }

    // main method
    public static void main(String[] argv)
    {
        double x = 5.0;

        printSquare(3.0);   // prints the square of 3.0 to the screen
        printSquare(x);     // prints the square of x to the screen
    }
}

Variable y, which is declared at the same time as the printSquare method, only exists within this method. Each time the method is called, the variable is recreated, and then ceases to exist when flow returns to the main method. Code outside of the printSquare method cannot use y. That is, the scope of y is the method printSquare. The concept of scope was introduced in section 3, but now it has to be extended to include methods and classes.

Similarly x is defined inside the main method, and may only be used in there. The scope of x is the main method. So x and y exist independently of each other, within their respective methods.12 They are known as method variables. It is not possible for printSquare to use the variable x, or for the main method to use y, because the variables do not exist there.

Before this section on multiple methods, all the variables you have used have either been declared in the main method (in which case their scope is from their declaration to the end of the method), or within a for loop, if statement or similar block of code (in which case their scope was from declaration to the end of the code block, denoted by the closing curly bracket }). Now you have also created variables in other methods, but the principle is just the same.

There is one further level at which variables can be declared. These are class variables. They are variables declared outside of any methods, but within the class, and as such exist for all methods in the class. Their basic declaration is the same as with any variable, however like methods they should have public static added to the beginning. Here is a simple program which does something similar to the previous example, but where the scope of x is different.

/**
 * Purpose: Example of program with a class variable 
 *          The program prints the value of x squared
 *          to the screen.
 */

public class ScopeExample
{
    // declare class variable x
    public static double x;

    // method to print x squared to screen
    public static void printXSquare()
    {
        System.out.println(x*x);
        return;
    }

    // main method
    public static void main(String[] argv)
    {
        // give x a value and print its square to screen
        x = 5.0;
        printXSquare();

        // change value of x and print square to screen
        x = 2.0;
        printXSquare();
    }

}

Since x exists within the entire class, both methods can make use of it.

There is no need to pass the value of x to the method printXSquare, so there is no need for the method to take an argument. Note that because printXSquare has been written to print the value of x2 to the screen, it can not be used to print the square of any other value. It is not in general a very useful method.

To summarise these ideas of variable scope:

Class Variables
An example is the variable a in the code below. For now you should always declare using public static. The variable is declared in the class, but outside of the methods. It is visible to all the the class, so all the methods of the class may use it.

Method Variables
Examples are b and c in the code below. Method variables are declared inside a method (c), or as an argument in a method declaration (b). The scope of c is from its declaration to the end of the method. The scope of b is the entire method.

Variables declared in a block of code
E.g. the variables d and e in the code below. They are normally declared within a block of code (code within a pair of curly brackets {}) and their scope is from declaration to the end of that block of code. In the case of a for loop the declaration in in the round brackets following for and the variable exists for the duration of the loop.

/**
 * Purpose:  Program that does nothing useful, but using variables
 *           that have a variety of scopes.
 */

public class ScopeSummary
{
    // scope of a is the entire class
    public static double a;

    // scope of b is all of method alpha
    public static double alpha(int b)
    {
        double c;              // scope of c from here to method end
        c = a*b;
        return c;
    }

    public static void main(String[] argv)
    {
        // scope of d is all of for loop
        for (int d=1; d<100; d*=2)
        {
            if ((d%3) != 0)
            {
                a = d%3.0 + 1.234;
                double e = alpha(d);    // scope of e starts here
                System.out.println(e);
            }                           // scope of e ends here
        }
    }
}

Exercise 6.3


(4 marks)



With reference to the example code above, categorise the following statements as true or false.

(i)
The variable a is visible in any method of the class.
(ii)
The variable d is visible inside the method alpha.
(iii)
The variable c is not visible inside the method main.
(iv)
The variable e is visible anywhere inside the method main.




next up previous contents
Next: Passing arrays to methods Up: 6 Multiple methods Previous: Method naming   Contents
RHUL Dept. of Physics