Cannot find symbol Java

In this post, we will see how to solve Cannot find symbol in java.

You will get this error when you have declared something that compiler does not understand. The compiler creates list of identifiers when you compile any program. If compile can not understand any of the identifiers, you will get this error.

This error generally means the compiler does not recognize any identifier and can not figure out what it means.

Let’s understand with a very basic example.

You will get below compilation error.

CannotFindSymbolMain.java:11: error: cannot find symbol
division = a/ b;
symbol: variable division
location: class CannotFindSymbolMain
CannotFindSymbolMain.java:12: error: cannot find symbol
System.out.println(division);
symbol: variable division
location: class CannotFindSymbolMain
2 errors

Did you understand the issue?
We are getting this error because we did not declare division before using it.
You need to change line 11 to:

This will resolve above compilation error.

There can be multiple reasons for this error.

  1. You forgot to declare any variable
  2. You misspelled any methodname, class, variable or interface. Please note that java is case sensitive language, so division and Division will be not be considered same.
  3. You did not import any class correctly
  4. You forgot new keyword while initializing an object.
    We forgot to use new, so it should be
  5. There can be an issue with source file encoding. Even though Identifier looks exactly same, but there can hidden characters

As you can see, there can be multiple reasons for this error. You need to figure out what can be the reason for this error in your program. In most of the cases, this error is due to either the undeclared variable or misspelled identifier.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *