What is the Cannot Find Symbol Java Error?

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

The Cannot Find Symbol in Java is a compilation error that occurs when we refer to something not present in the Java Symbol Table. A common example of this error is using a variable not declared in the program.

Java compilers create and maintain Symbol tables. The symbol table stores information about the identifiers, i.e., classes, interfaces, variables, and methods in the given source of code. When we use these identifiers in our code, the compiler looks to the symbol table for information. If the identifier is not declared or is not present in the given scope, the compiler throws 'Cannot Find Symbol'.

What Causes the Java Cannot Find Symbol Error?

There are multiple ways and reasons this error can occur, they all boil down to the fact that the online Java compiler couldn't find the identifier in the Symbol table. Some of the very commonly made mistakes are listed below:

  • Java is case sensitive, thus hello and Hello are two different variables.
  • If identifiers are misspelt, say the variable's name is name, and we are trying to access name can cause an error.
  • Sometimes variables are declared in a particular iterating loop or method, and if we try to call them from outside, it causes an error as it is out of the variable's scope.
  • Using variables without their declaration is also the common cause of the Java 'Cannot find Symbol' error.
  • Sometimes, we use methods from classes in a different package. To do so, we are required to import the class's package. Missing import statements can also cause the error.
  • The use of an underscore, dollar sign, number, letter, or even a character before or after the initial declaration that is different from the initial declaration can cause 'Cannot Find Symbol'.

Examples of Java Cannot Find Symbol error

Let us now see these common errors in the form of code:

Example 1

Let us take a very simple example of a function that takes user input of a number and returns if the number is even or odd.

Output:

In the example above, the Scanner class is not imported, and thus, it is throwing a Cannot Find Symbol error. The compiler is not able to find Scanner in the program. Once we import the package, it can identify the scanner class.

Example 2

Another example is wrong spelling and undeclared variables. Let us take a simple example where we multiply a variable a few times in a loop.

Output:

In the example above, we can see three errors, all of them 'cannot find symbol'. The first one is because N is not declared in the program, and the two are because we declared finalResult but are calling final_Result. Thus, a single underscore can also cause an error.

Example 3

Lastly, out-of-scope variables also cause a 'Cannot find Symbol' error, below example demonstrates it.

Output:

In the example above, the sum variable is declared inside the for loop and its scope is only inside for loop. As we are trying to access it outside the for loop, it is causing the error.

Structure of Java Cannot Find Symbol

Look at the output generated by the compiler for the 3rd example:

output-generated-bycompiler

As we can see in the output, the compiler tells the line number of the error as well as points to the variable where the error occurred and the type of the error which is cannot find symbol in this case. The compiler also produces the cannot find symbol including these two additional fields:

  • The very first one is the symbol it is unable to find, i.e. the name and type of the referenced identifier.
  • The other field is the location of the place where the symbol is used. The class and line number are returned where the identifier has been referenced.

Cannot Find Symbol vs Symbol Not Found vs Cannot Resolve Symbol

Different compilers may use slightly different terminologies. The 'Cannot Find Symbol', 'Symbol Not Found', and 'Cannot Resolve Symbol' are all same errors only. Besides the naming, these convey the same meaning to the user and the compiler.

Learn more about Java errors

Conclusion

  • The "Cannot Find Symbol" error in Java occurs during compilation when the compiler encounters an identifier (such as a variable or method name) that it cannot recognize or resolve.
  • Silly mistakes like case sensitivity, use of underscore, use of undeclared variables, use of out-of-scope variables, etc. can cause this error.
  • It is also identified by 'Symbol Not Found' and 'Cannot Resolve Symbol'.