This post features solutions to fix the Cannot find Symbol error in Java. It is a compilation error that occurs when the compiler can’t find a reference to an identifier. Fortunately, you can follow some simple suggestions to fix the error.
Fix Cannot find Symbol error in Java
To fix the Cannot find Symbol error in Java, follow these suggestions:
- Check for Typing Errors
- Undeclared or Out of Scope Variable
- Missing Import Statement
Now, let’s see these in detail.
1] Check for Typing Errors
Start by checking for any typing errors in your program. In our experience, typos are the most common reason why the Cannot find Symbol error in Java occurs. Here’s an example:
public class TypoExample { public static void main(String[] args) { int number = 10; System.out.println("The value is: " + numer); // Typo 1 String greeting = "Hello, World!"; System.out.println(greetting); // Typo 2 } }
Output:
TypoExample.java:5: error: cannot find symbol System.out.println("The value is: " + numer); // Typo 1 ^ symbol: variable numer location: class TypoExample TypoExample.java:7: error: cannot find symbol System.out.println(greetting); // Typo 2 ^ symbol: variable greetting location: class TypoExample 2 errors
We can see that in Typo 1, number is written as numer. And in Typo 2, greeting is written as greetting. Here’s the corrected code:
public class TypoExample { public static void main(String[] args) { int number = 10; System.out.println("The value is: " + number); // Fixed typo 1 String greeting = "Hello, World!"; System.out.println(greeting); // Fixed typo 2 } }
Output:
The value is: 10 Hello, World!
2] Undeclared or Out of Scope Variable
The cannot find symbol error in a Java program can also occur due to undeclared or out-of-scope variables. Here’s a Java program demonstrating the error:
public class ScopeDemo { public static void main(String[] args) { int x = 5; System.out.println("The value is: " + y); // Undeclared variable // Out-of-scope variable if (x > 0) { int z = 10; } System.out.println("The value of z is: " + z); // Out-of-scope variable } }
Output:
ERROR! javac /tmp/v1FN2QQUVZ/ScopeDemo.java /tmp/v1FN2QQUVZ/ScopeDemo.java:8: error: cannot find symbol System.out.println("The value is: " + y); // Undeclared variable ^ symbol: variable y location: class ScopeDemo /tmp/v1FN2QQUVZ/ScopeDemo.java:14: error: cannot find symbol System.out.println("The value of z is: " + z); // Out-of-scope variable ^ symbol: variable z location: class ScopeDemo 2 errors
In this code, the variable y is being used without proper declaration. Also, the variable z is within an if block; this makes it out of scope when accessing it outside the block, resulting in the Cannot find Symbol error. The corrected code is:
public class ScopeDemo { public static void main(String[] args) { int x = 5; System.out.println("The value is: " + x); // Fixed variable name // Moved the declaration to the outer scope int z = 0; if (x > 0) { z = 10; } System.out.println("The value of z is: " + z); // Fixed variable scope } }
Output:
The value is: 5 The value of z is: 10
3] Missing Import Statement
Import statement in Java helps make a class or all classes visible for a specified program under a package using a single statement. If any classes or packages are not imported properly, it can trigger the Cannot find Symbol error in Java. Here’s a sample program demonstrating the error:
public class ImportDemo { public static void main(String[] args) { // Missing import statement for Scanner Scanner scanner = new Scanner(System.in); System.out.print("Enter a number: "); int number = scanner.nextInt(); System.out.println("You entered: " + number); } }
Output:
ERROR! javac /tmp/v1FN2QQUVZ/ImportDemo.java /tmp/v1FN2QQUVZ/ImportDemo.java:7: error: cannot find symbol Scanner scanner = new Scanner(System.in); ^ symbol: class Scanner location: class ImportDemo /tmp/v1FN2QQUVZ/ImportDemo.java:7: error: cannot find symbol Scanner scanner = new Scanner(System.in); ^ symbol: class Scanner location: class ImportDemo 2 errors
Here, we’re trying to use the scanner class without importing it. Adding the import statement, i.e., “import java.util.Scanner”, will make the prgram to run successfully. Here’s the corrected one:
import java.util.Scanner; public class ImportDemo { public static void main(String[] args) { // Fixed import statement for Scanner Scanner scanner = new Scanner(System.in); System.out.print("Enter a number: "); int number = scanner.nextInt(); System.out.println("You entered: " + number); } }
Output:
Enter a number: 2 You entered: 2
Read: Java not working in Windows 11
We hope these suggestions help you fix the error.
What is the Cannot resolve symbol?
In Java, the Cannot resolve symbol error indicates that the compiler can’t recognize the name used in the code. Usually, this occurs when can’t import the class, accidental typo, or if the class doesn’t exist.
Read: Java Application Blocked by Java Security
What does an unknown symbol error usually indicate Java?
The Unknown symbol error occurs if the Java compiler encounters a reference to a symbol, like a variable or method name, that it can’t recognize. This indicates there is a typo in the code, or it’s not in scope.