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

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

王林
Release: 2023-09-15 12:57:03
forward
3266 people have browsed it

在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);
   }
}
Copy after login

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
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template