The “cannot find symbol” occurs mainly when we try to reference a variable which is not declared in the program code which we are compiling,it means that the compiler doesn’t know the variable we are referring to.
During compiling,the compiler needs to know what each and every identifier refers to in your code but if it doesn’t it means that the compiler cannot comprehend what that symbol means.
Some possible causes for “Cannot find symbol” to occur are-
Using a variable which is not declared or outside your code.
2. Using wrong cases(“halo” and “Halo are different) or making spelling mistakes.
3. The packaged class has not been referenced correctly using an import declaration.
4. Using improper identifier values (letters, numbers ,underscore ,dollar sign),hello-class is different from helloclass.
Example-
public class Test {
public static void main(String[] args) {
int a = 2;
int b = 4;
sum = (a + b );
System.out.println(sum);
}
}
Here, Cannot find symbol error will occur because “sum” is not declared .In order to solve the error we need to define “int sum” before using the variable sum.
Solution-
public class Test {
public static void main(String[] args) {
int a = 2;
int b = 4;
int sum;
sum = (a + b );
System.out.println(sum);
}
}