“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 4, 2018 in Java by developer_1
• 3,280 points
3,792 views

2 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 4, 2018 by Rishabh
• 3,540 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 by patricblues
• 140 points

Related Questions In Java

0 votes
0 answers

how to solve thread-cannot find symbol

Feb 11 in Java by anonymous
25 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 parth295
• 4,600 points
83 views
+2 votes
12 answers

“A Non-static method cannot be referenced from a static context” error

this method must be static so that it will exist even though the ...READ MORE

answered Jun 11 in Java by Neha
• 330 points
34,963 views
0 votes
1 answer

Unable to find valid certification path to requested target - error even after cert imported

Unfortunately - it could be many things ...READ MORE

answered Nov 27, 2018 in Java by Daisy
• 8,040 points
671 views
0 votes
1 answer

What is a StackOverflow Error

Parameters and local variables are allocated on ...READ MORE

answered May 21, 2018 in Java by parth295
• 4,600 points
56 views
0 votes
1 answer

Non-static variable cannot be referenced from a static context. Explain?

Static fields and methods are connected to ...READ MORE

answered May 21, 2018 in Java by parth295
• 4,600 points
3,819 views
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,840 points
951 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,540 points
699 views
+9 votes
11 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,540 points
22,786 views