In Java while programming, there is a common error which the developers faces and it is,
” javac : Cannot find Symbol“
These errors are due to variables going out of their scope. Scoping errors come in many shapes and sizes. One common mistake happens when a variable is shadowed and two scopes overlaps. Just go through Scope of Variable to revise your concept on Java variables and their scopes.
The most common reason for scoping errors is when you attempt to access a variable that is not in scope. In this post, I will note down the three most common errors which appears due to the variables scope.
Attempting to access an instance variable from a Static Context :
class MyClass {
int x = 3;
public static void main ( String[] args ) {
x++;
// This would not compile because x is an ‘instance’ variable, it could not be accessed //from within a static context.
}
}
Accessing Local variable from Nested Method :
class MyClass {
public static void main ( String[] args) {
MyClass c = new MyClass(); // reference variable c referring to an Object of class MyClass
c.methodA();
}
void methodA() {
int x = 4 ;
methodB();
x++; // Once methodB completes x comes back in scope.
}
void methodB () {
x++; // would not compile, In method B, scope of variables of method A (ie x) ends.
}
}
Using a block variable, out of the block :
void MyMethod() {
for (int i =0; i<=5 ; i++){
boolean test; // By default a boolean variable is given the value ‘false’
}
System.out.print(test); // would not compile, test variable has it’s scope only within //the for loop
}
In the last two examples the compiler will say something like this :
“cannot find symbol”
References :
1. Scope of Variable : http://ezdia.com/Scope_of_Variables_in_Java/Content.do?id=1300
2. Java Static Variables : http://ezdia.com/Java_Static_Variable/Content.do?id=1301

February 21, 2010 at 6:54 am
Good dispatch and this fill someone in on helped me alot in my college assignement. Gratefulness you for your information.