Tutorial Java SCJP – #10 Variable Scope

Many compiler errors are generated because programmers don’t have a clear image on how long variables are available and when they can access them. The variable scope concept  describes the life of a variable, or its availability area, after it was defined.

Other topics that are part of this Java tutorial are accessible through Java 6 Tutorial – Contents.

In order to understand this topic, let’s consider an example:

class Student{
    public static int noStudents = 0;  //static class variable
    public int age;              //instance variable
    public int[] marks;     //instance variable

    public Student()
    {
        noStudents++;   //accessing static variables
        this.age = 0;
        marks = new int[10];
    }
    public float getAverage()
    {
        float sum = 0;    //local variable
        for(int i = 0;i < marks.length;)
        {   //i is local variable in the for block
            sum+=marks[i];
            i++;
        }
        return sum/marks.length;
    }
}

public class Main{
	public static void main(String[] args)
        {
            Student s1 = new Student();
            Student s2 = new Student();

            //accessing static variables
            System.out.println("The are "+Student.noStudents+" students");
	}
}

Each type of variable has its own scope:

Instance variables (attributes) - these variables are part of an object, so, they are created when the object is created; they exists until the object is created; the object and its methods have access to its instance variables;

Static variables - these variables are part of a class; they are created when the class is loaded by the JVM;

Methods local variables - these variables are defined on the method stack and they exists as long as the method is executed (it is placed on the call stack); even the local variables can be accessed, you can't use them from a nested method (Tutorial Java – #8 Understand Stack and Heap);

Block variables - these variables are defined inside blocks of code (between { and }) and can be used while the block is executed; typical blocks of code are for, while, initialization block.

What are common errors regarding variables scope - Cannot find symbol compiler error

Cannot find symbol compiler error is generated when you try to use a block variable outside the block:

    public float getAverage()
    {
        float sum = 0;    //local variable
        for(int i = 0;i < marks.length;)
        {   //i is local variable in the for block
            sum+=marks[i];
            i++;
        }
			//Cannot find symbol compiler error
        i = 0;          //COMPILER ERROR
        return sum/marks.length;
    }

Another case in which you get the same Cannot find symbol compiler error is when you try to access a method local variable from a nested method:

    public static void DoSomething()
    {
            int variable = 0;       //local variable
            DoSomethingElse();	    //nested method
    }
    public static void DoSomethingElse()
    {
	//Cannot find symbol compiler error
        variable++;     //COMPILER ERROR
    }

You get the same error when you try to access instance variables or methods from a static context, like the main method.

	public static void main(String[] args)
        {
            Student s1 = new Student();
            Student s2 = new Student();

            //accessing static variables
            System.out.println("The are "+Student.noStudents+" students");

	    //Cannot find symbol compiler error
            age = 23;         //COMPILER ERROR
            getAverage();     //COMPILER ERROR
	}

Other topics that are part of this Java tutorial are accessible through Java 6 Tutorial – Contents.