What is the "cannot find symbol" error in Java?

Share

The “cannot find symbol” error is caused when a user references a variable that is not declared in the program. In simpler words, the compiler is unaware of any declaration of said variable.

class HelloWorld {
public static void main( String args[] ) {
int a = 5;
int b = 4;
// product is not declared. This will show an error.
product = a * b;
}
}

How to fix the error

You should check the code for the following scenarios if you ever come across this error:

  1. Check if you have declared the variable, i.e., int i = 3;. If someone has not written int, then the error may appear. Also, check if the variable declared is outside the code, e.g., if it is not part of the HelloWorld class.

  2. Ensure that you are using the correct case. An example could be if you declared the variable as var but are accessing it as Var.

  3. Identifier values like numbers, dollar signs, and hyphens are not allowed – check for them.

Copyright ©2024 Educative, Inc. All rights reserved