# What can cause the "cannot find symbol" error in Java?

The “cannot find symbol” error occurs mainly when we try to reference a variable that is not declared in the program which we are compiling, it means that the compiler doesn’t know the variable we are referring to.

## Some possible causes for “Cannot find symbol” to occur are

• Using a variable that is not declared or outside the code.
• Using wrong cases (“tutorials” and “Tutorials" are different) or making spelling mistakes.
• The packaged class has not been referenced correctly using an import declaration.
• Using improper identifier values like letters, numbers, underscore and dollar sign. The hello-class is different from helloclass.

## Example

public class CannotFindSymbolTest {
public static void main(String[] args) {
int n1 = 10;
int n2 = 20;
sum = n1 + n2;

System.out.println(sum);
}
}

## Output

CannotFindSymbolTest.java:5: error: cannot find symbol
sum = n1 + n2;
^
symbol: variable sum
location: class CannotFindSymbolTest
CannotFindSymbolTest.java:7: error: cannot find symbol
System.out.println(sum);
^
symbol: variable sum
location: class CannotFindSymbolTest

In the above program, "Cannot find symbol" error will occur because “sum” is not declared. In order to solve the error, we need to define “int sum = n1+n2” before using the variable sum.

Published on 31-Jul-2019 22:40:44