Solve Error can not find symbol in Java

In this article, we’ll see the reason behind the error can not find symbol or can not resolve symbol in Java So you can solve it by removing that cause. So let’s start.

A java program consists of keywords, literals, operators, comments, whitespaces, and identifiers. This error is related to identifiers (an identifier is a name that user assign to an element of a program such as a type, function, variable, namespace, or class). Because when you compile your code, the compiler needs to know about each of the identifiers that we have used, and when we put an identifier that refers to something, and the compiler doesn’t understand that so it throws the error “can not find symbol”.

Reason 1: Using an Undeclared Variable

Example:

Output:

Demo.java:8: error: cannot find symbol

        sum = n1+n2;

        ^

  symbol:   variable sum

  location: class Demo

In the above program, we are using the sum variable to store the sum of n1 and n2 variables and our program is throwing the error “cannot find symbol” and it is pointing to the sum variable. So we can easily solve it by declaring the sum variable before using it.

So make sure your code won’t be having this mistake.

Reason 2: Spelling Mistakes or Using Wrong Cases

Example:

Output:

 Demo.java:10: error: cannot find symbol

             System.out.println(“Sum= “+ Sum);

                                    ^

              symbol:   variable Sum

              location: class Demo

Again this is one of the most common reasons for this error. Java is a case sensitive language which means Sum is different from the sum. So we have to use the exact name in the same case format as we have declared it to prevent this error.

Reason 3: Variable is Out of Scope

Example:

Output:

Demo.java:11: error: cannot find symbol

        System.out.println(“loop breaked at – ” + i);

                                                  ^

  symbol:   variable i

  location: class Demo

1 error

So here we can see that even we have declared the variable i and still it is showing “cannot symbol error” because we have declared it inside for loop and trying to use it outside of for loop. It’s called out of scope. To prevent this make sure that your variable is defined in the same scope where you’re using it. Like in the above program to use i outside of the for loop i have declared i inside the main function.

Reason 4: Forgot to Import Class

Example:

Output:

 error: cannot find symbol

        ArrayList a = new ArrayList();

        ^

  symbol:   class ArrayList

  location: class Demo

Here, we are using the correct spelling of ArrayList to use it. But still, it says  “cannot find symbol”. Again compiler couldn’t find the meaning of the ArrayList because ArrayList exists in a package “java.util”, so to use it we have to import ArrayList from this package. It is again a common mistake we usually do by using a class or method without importing it in our program. If you’re using an editor like visual studio code or eclipse you can fix it by hovering on the ArrayList and it will show the package to import. But if you’re using a simple text editor then you have to manually write a line at the top of your java file.

import java.util.ArrayList;

So make sure for the next time, you’ve imported all the classes that you’re using in your program.

Reason 5: Using a Method that is Undefined for the Class
Example:

Output:

Demo.java:8: error: cannot find symbol

       System.out.println(s.size());

                           ^

  symbol:   method size()

  location: variable s of type String[]

1 error

Here is another example that cause the same error. Here i am trying to get the size or length of the string using size() method which is not a String class’s method. It is used to get the size of an array, not the string that’s why it is showing this error. So make sure that the methods we’re using are defined for the class or not. In the above case the right we can use length property instead of size() method to get the size of a string.

So these were some reasons when we face cannot find symbol error. I hope this article has helped you to solve this. If you have any queries or suggestions related to this article please comment below.

Leave a Reply

Your email address will not be published. Required fields are marked *