Whenever a Compiler does not recognize a class name, Java displays an error ?cannot find symbol?.
Whenever a Compiler does not recognize a class name, Java displays an error “cannot find symbol”.
The reason behind cannot find symbol error is:
- A programmer has misspelled the name of the class.
- A programmer didn’t Imported the class name.
Following example will help you understand the java error cannot find symbol. In this example a class name 'cannot find symbol' is used.
int variable “w” and “x” with respective values have been initialized. int “y” stores the sum of their value.
The System.out.println is used to print the output. But programmer instead of writing “y”, wrote variable “z”.
The compiler did not recognize a variable “z” and displays an error “Java error cannot find symbol”.
Cannotfindsymbol.java
import java.lang.*; public class Cannotfindsymbol { public static void main(String[] args) { try{ int w=10; int x=3; int y=w+x; System.out.println(z); } catch(Exception ex){ System.out.println(ex); } } }
Output:
/home/girish/NetBeansProjects/errors/src/
Cannotfindsymbol.java:11: cannot find symbol
symbol: variable z
location: class Cannotfindsymbol
System.out.println(z);
1 error0
*To remove this error you have to declare the variable z.