Back

Explore Courses Blog Tutorials Interview Questions
+7 votes
5 views
in Java by (220 points)
recategorized by

Explain the following about the "Cannot find symbol" error:

  1. This error signifies what?
  2. State the reasons to hit this error.
  3. State the steps/approach programmers should go with to address this error.

The above question is a compilation error, "cannot find symbol" in Java. 

3 Answers

+15 votes
by (13.2k points)
edited by

The “cannot find symbol” occurs mainly when we try to reference a variable which is not declared in the program code which we are compiling,it means that the compiler doesn’t know the variable we are referring to.

During compiling,the compiler needs to know what each and every identifier refers to in your code but if it doesn’t it means that the compiler cannot comprehend what that symbol means.

Some possible causes for “Cannot find symbol” to occur are-

  1. Using a variable which is not declared or outside your code.

2. Using wrong cases(“halo” and “Halo are different) or making spelling mistakes.

3. The packaged class has not been referenced correctly using an import declaration.

4. Using improper identifier values (letters, numbers ,underscore ,dollar sign),hello-class is different from helloclass.

Example-

public class Test {

       public static void main(String[] args) {

           int a = 2;

           int b = 4;

           sum = (a + b );

           System.out.println(sum);

       }

   }

Here, Cannot find symbol error will occur because “sum” is not declared .In order to solve the error we need to define “int sum” before using the variable sum.

Solution-

public class Test {

       public static void main(String[] args) {

           int a = 2;

           int b = 4;

          int sum;

           sum = (a + b );

           System.out.println(sum);

       }

   }

+6 votes
by (1.4k points)
edited by

Cannot find symbol error occurs only when the compiler cannot find the definition as it searched through all the places where the identifier should be defined.

It can be caused due to variety of things, some of which are most common to encounter are-

1) One of the most common mistakes are spelling the name wrong, i.e. “StringBaffer” instead of “StringBuffer”

2) Another common mistake is using a variable that was declared somewhere else, i.e. In a different context altogether, which might be a different scope.

3) Another mistake that happens often is getting the case wrong, which means not caring for case sensitiveness of the java language. i.e. “StringBuffer” is not same as “stringbuffer”.

If you get that error message, first up what you want to do is find that code line where you get the error message, then you will be able to find which variable, class or what the exact issue is with the particular line of code. Hope that helps!

0 votes
by (32.3k points)
edited by

Answers given by @Shivangi and @Franklin are fine  You may also get this error if you forget a new:

String s = String();

Want to learn Java from scratch! Have a look at this end to end video on Java:

versus

String s = new String();

Related questions

0 votes
1 answer
asked Jan 24, 2021 in Java by dante07 (13.1k points)
0 votes
1 answer
0 votes
1 answer
asked Jun 5, 2021 in Java by sheela_singh (9.5k points)

Browse Categories

...