Home  >  Article  >  Java  >  What are the causes of 'cannot find symbol' errors in Java?

What are the causes of 'cannot find symbol' errors in Java?

王林
王林forward
2023-09-15 12:57:033194browse

在Java中,"cannot find symbol"错误的原因有哪些?

Mainly occurs when we try to reference a variable that is not declared in the program we are compiling "Symbol not found" error, which means The compiler has no idea what variables we are referencing.

Some possible reasons why "symbol not found" occurs include:

  • Using an undeclared variable or using it outside the code.
  • uses incorrect capitalization ("tutorials" and "Tutorials" are different) or is misspelled.
  • The import statement is not used correctly to reference the packaged class.
  • Use of incorrect identifier values ​​such as letters, numbers, underscore and dollar sign. hello-class is different from helloclass.

Example

public class CannotFindSymbolTest {
   public static void main(String[] args) {
      int n1 = 10;
      int n2 = 20;
      sum = n1 + n2;
      System.out.println(sum);
   }
}

Output

CannotFindSymbolTest.java:5: error: <strong>cannot find symbol
</strong>sum = n1 + n2;
^
symbol: variable sum
location: class CannotFindSymbolTest
CannotFindSymbolTest.java:7: error: <strong>cannot find symbol</strong>
System.out.println(sum);
^
symbol: variable sum
location: class CannotFindSymbolTest

In the above program, "Find Symbol not found" error because " sum" was not declared. To solve this error, we need to define "int sum = n1 n2" before using the variable sum.

The above is the detailed content of What are the causes of 'cannot find symbol' errors in Java?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete