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

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



In Java, the cannot find symbol error occurs when we try to reference a variable that is not declared in the program or when we try to access a class that is not declared or imported. 

Possible Causes of 'Cannot Find Symbol' Error

Below is a list of some possible causes of the 'cannot find symbol' error in Java -

  • 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 1

If a variable is not declared in Java, the compiler will throw a cannot find symbol error.

In the following program, we are trying to print the value of the variable sum, which has not been declared. As a result, the compiler will throw a cannot find symbol error -

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

Output

The above program throws the following error -

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

Example 2

If a class from a package is not correctly referenced using an import statement in Java, the compiler will throw a cannot find symbol error.

Here is another program that also causes a cannot find symbol error. We are instantiating the Scanner class, but we have not imported the package java.util.Scanner so that the compiler will throw an error -

public class main{
   public static void main(String[] args) {
      Scanner sc = new Scanner(System.in);
      int n = sc.nextInt();
      System.out.println(n);
   }
}

Output

The above program throws the following error -

main.java:3: error: cannot find symbol
Scanner sc = new Scanner(System.in);
    ^
symbol:   class Scanner
location: class main
main.java:3: error: cannot find symbol
    Scanner sc = new Scanner(System.in);
                     ^
symbol:   class Scanner
location: class main
2 errors

Example 3

Since Java is a case-sensitive language, the compiler will throw a cannot find symbol error if a variable is referenced using a different case than it was declared -

public class main {
   public static void main(String[] args) {
      String name = "xyd";
      System.out.println(Name);
   }
}

Output

Following is the output of the above program -

main.java:4: error: cannot find symbol
System.out.println(Name);
                       ^
symbol:   variable Name
location: class main
1 error
Updated on: 2025-05-29T05:39:49+05:30

12K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements