Cannot find symbol compilation error

0 votes

Please explain the following about the "Cannot find symbol" error:

  • What does this error mean?
  • What things can cause this error?
  • How does the programmer go about fixing this error?

This question is designed to be a comprehensive question about "cannot find symbol" compilation errors in Java.

Jun 5, 2018 in Java by developer_1
• 3,350 points
42,404 views

4 answers to this question.

0 votes

As I've seen that kind of questions a few times already, maybe one more example to what's illegal even if it might feel okay.

Consider this code:

if(somethingIsTrue()) {
  String message = "Everything is fine";
} else {
  String message = "We have an error";
}
System.out.println(message);

That's invalid code. Because neither of the variables named message is visible outside of their respective scope - which would be the surrounding brackets {} in this case.

You might say: "But a variable named message is defined either way - so message is defined after the if".

But you'd be wrong.

Java has no free() or delete operators, so it has to rely on tracking variable scope to find out when variables are no longer used (together with references to these variables of cause).

It's especially bad if you thought you did something good. I've seen this kind of error after "optimizing" code like this:

if(somethingIsTrue()) {
  String message = "Everything is fine";
  System.out.println(message);
} else {
  String message = "We have an error";
  System.out.println(message);
}

"Oh, there's duplicated code, let's pull that common line out" -> and there it it.

The most common way to deal with this kind of scope-trouble would be to pre-assign the else-values to the variable names in the outside scope and then reassign in if:

String message = "We have an error";
if(somethingIsTrue()) {
  message = "Everything is fine";
} 
System.out.println(message);
answered Jun 5, 2018 by Rishabh
• 3,620 points
0 votes

The "Cannot find symbol" errors generally occur when you try to reference an undeclared variable in your code. A "Cannot find symbol" error means that the compiler cannot do this. Your code appears to be referring to something that the compiler doesn't understand.

When your code is compiled, the compiler needs to work out what each and every identifier in your code means. As the compiler is going through the code it will find something and know what to do with it or not. Your Cannot find symbol error relates to the identifiers and means that Java cannot figure out what the "symbol" means.

The general causes for a Cannot find symbol error are things like:

  • Incorrect spelling.
  • Wrong case. Halo is different from halo.
  • Improper use of acceptable identifier values (letters, numbers, underscore, dollar sign), my-class is not the same as myclass.
  • No variable declaration or variable is outside of the scope you are referencing it in.
answered Apr 3, 2019 by patricblues
• 140 points
0 votes

A "Cannot find symbol" error is about the identifiers. When your code is compiled, the compiler needs to work out what each and every identifier in your code means.

A "Cannot find symbol" error means that the compiler cannot do this. Your code appears to be referring to something that the compiler doesn't understand.

answered Dec 16, 2020 by Rajiv
• 8,870 points
0 votes

The "Cannot find symbol" errors generally occur when you try to reference an undeclared variable in your code. A "Cannot find symbol" error means that the compiler cannot do this. Your code appears to be referring to something that the compiler doesn't understand.

When your code is compiled, the compiler needs to work out what each and every identifier in your code means. As the compiler is going through the code it will find something and know what to do with it or not. Your Cannot find symbol error relates to the identifiers and means that Java cannot figure out what the "symbol" means.

Example

public class TestClass {
  public static void main(String[] args) {
    int x = 2;
    int y = 4;
    sum = x + y ;
    System.out.println(sum);
  }
}

output

TestClass.java:10: error: cannot find symbol
            sum = x + y ;
  symbol:   variable sum
  location: class TestClass
TestClass.java:11: error: cannot find symbol
            System.out.println(sum);
  symbol:   variable sum
  location: class TestClass
2 errors

answered Dec 16, 2020 by Gitika
• 65,770 points

Related Questions In Java

0 votes
0 answers

how to solve thread-cannot find symbol

Feb 11, 2019 in Java by anonymous
1,088 views
0 votes
0 answers

what is cannot find symbol this(SimpleTimeLimiter.create(THREAD_POOL));

Hi, Through command line, I'm trying to compile ...READ MORE

Nov 6, 2020 in Java by zaraaq
• 270 points

edited Nov 6, 2020 by Gitika 2,058 views
0 votes
0 answers

CreateProcess error=2, The system cannot find the file specified

I have written a java program to ...READ MORE

May 5, 2022 in Java by Kichu
• 19,040 points
6,392 views
0 votes
1 answer

How to resolve the error: could not find or load main class?

If you are getting error: could not ...READ MORE

answered May 28, 2018 in Java by Parth
• 4,640 points
11,431 views
0 votes
2 answers

What is a StackOverflow Error

stackoverflow error – StackOverflow Error in Java. This error is thrown to ...READ MORE

answered Aug 29, 2020 in Java by Pistle
• 1,000 points
2,136 views
+1 vote
2 answers
0 votes
2 answers

Connection reset : java.net.SocketException

You should check whether the client program is ...READ MORE

answered Sep 6, 2018 in Java by Sushmita
• 6,920 points
5,881 views
+1 vote
1 answer
0 votes
1 answer

Following error is occurring "Can't start Eclipse - Java was started but returned exit code=13"

There are combinations of Operating System, JDK ...READ MORE

answered Apr 19, 2018 in Java by Rishabh
• 3,620 points
2,977 views
+9 votes
16 answers

Unable to resolve this error: "javac is not recognized as an internal or external command"

Check your javac path on Windows using Windows Explorer C:\Program Files\Java\jdk1.7.0_02\bin and ...READ MORE

answered May 23, 2018 in Java by Rishabh
• 3,620 points
463,850 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP