When a symbol, variable, or method is absent from the code, the program is said to be in a “cannot find symbol” error condition in Java. This means if the compiler cannot find something in the program that is being called then it will show the cannot find symbol error.
Java compiler creates and manages the “Symbol table” that stores all the symbols, variables, methods, classes, and interfaces which are declared in the source code. This makes checking for the reference class, symbol, variable, or function by the compiler simple. The Java compiler checks the reference object in the symbol table and if not declared then shows the cannot find symbol error.
We will address the Java “cannot find symbol” problem in this topic.
What is the Cannot Find Symbol Java Error?
When the identifier is either not defined or not present in the code, the “cannot find symbol” error happens. The error “cannot find symbol” has several sources. These causes are discussed below:
- There may be some undeclared variables in the code.
- The element being called may be out of scope.
- There may be some spelling mistakes.
- There may be a missing import statement.
Undeclared Variable Example
Check out the provided example to understand the undeclared variable concept:
class sum {
public static void main(String[] args) {
int a=10;
int b=5;
sum= a+b;
System.out.println(sum);
}
}
In this example,
- We have two variables a and b.
- We have called “sum” in the main method and printed it but we didn’t declare “sum” above.
The output is as follows:

Here, we can see that the compiler is showing a “cannot find symbol” error. If we declare “sum” in the program then check the output:
class sum {
public static void main(String[] args) {
int a=10;
int b=5;
int sum= a+b;
System.out.println(sum);
}
}
Output

Out-of-scope Element Example
Another cause of “cannot find symbol” error is calling element out of scope:
class loop {
public static void main(String[] args) {
for(int i=1;i<=10;i++){
int sum=0;
sum=sum+i;
System.out.println("sum is"+sum);
}
System.out.println("i is :"+i);
}
}
In this example:
- We have a class “loop” in which we have a for loop to print the sum.
- Inside the “for” loop, we have declared a “sum” variable and printed the sum of the first 10 integers.
- We have declared ”i” in a for loop. If we try to print “i” outside the for loop we will get an error because it’s out of the scope.
The output of the above-described code is:

But if we declare “i” outside of the loop we will get the desired output, let’s check out the provided code:
class loop {
public static void main(String[] args) {
int i;
for(i=1;i<=10;i++){
int sum=0;
sum=sum+i;
System.out.println("sum is: "+sum);
}
System.out.println("i is :"+i);
}
}
Output

Spelling Mistake Example
Java is a case-sensitive language that means “Hello” and “hello” are two different words. So, spelling mistakes can also cause cannot find symbol error:
class sum {
public static void main(String[] args) {
int a=10;
int b=5;
int sum=a+b;
System.out.println("Sum is: "+Sum);
}
}
In this example:
- We have used the same sum example. In the print statement, we called it “Sum” but we declared it as “sum” so it will show an error.
The output is:

By changing the “Sum” to “sum” we will get the output as:
class sum {
public static void main(String[] args) {
int a=10;
int b=5;
int sum=a+b;
System.out.println("Sum is: "+sum);
}
}

Missing “import” Statement Example
Another reason for “cannot find symbol” is missing “import” statement:
import java.util.Arrays;
public class example {
private static final List<String> names = Arrays.asList("Ali", "Sana", "Ahmed");
public static void main(String[] args) {
System.out.println(names);
}
}
In this example,
- We have declared a variable as “names” in which we have saved three names as a list but we didn’t import the java list library.
- The compiler will show an error because the list library is not imported.
Here is the output:

Let’s check out what happened when we import the list library:
import java.util.Arrays;
import java.util.List;
public class example {
private static final List<String> names = Arrays.asList("Ali", "Sana", "Ahmed");
public static void main(String[] args) {
System.out.println(names);
}
}
Output

We have provided the causes of the “cannot find symbol” error in Java with the aid of examples.
Conclusion
The Java “cannot find symbol” error occurs because of undeclared variables or methods that the compiler is unable to locate. There are different causes of “cannot find symbol” errors, such as undeclared variables, missing imports, spelling mistakes, or out-of-scope variables. This article explained the causes of the Java “cannot find symbol” problem.