java 错误找不到符号

When a Java program is being compiled, the compiler creates a list of all the identifiers in use. If it can't find what an identifier refers to (e.g., there is no declaration statement for a variable) it cannot complete the compilation.

编译Java程序时,编译器会创建一个所有正在使用的标识符的列表。 如果找不到标识符所指的内容(例如,没有变量的声明语句 ),则它将无法完成编译。

This is what the

这就是

cannot find symbol

error message is saying—the compiler doesn't have enough information to piece together what the Java code is intended to execute.

错误消息说-编译器没有足够的信息来拼凑Java代码打算执行的内容。

( Possible Causes For the "Cannot Find Symbol" Error )

Although the Java source code contains other things like keywords, comments, and operators, the "Cannot Find Symbol" error references the name of a specific package, interface, class, method or variable. The compiler needs to know what every identifier references. If it doesn't, the code is basically looking for something that the compiler doesn't yet comprehend.

尽管Java源代码包含关键字,注释和运算符之类的其他内容,但“找不到符号”错误引用了特定程序包,接口,类,方法或变量的名称。 编译器需要知道每个标识符都引用什么。 如果没有,则代码基本上是在寻找编译器尚未理解的东西。

Some possible causes for the "Cannot Find Symbol" Java error include:

Java错误“无法找到符号”的一些可能原因包括:

  • Trying to use a variable without declaring it. 尝试使用变量而不声明它。
  • Misspelling a class or method name. Remember that Java is case sensitive and spelling errors are not corrected for you. Also, underscores may or may not be necessary, so watch out for code that use them when they shouldn't be used or vice versa. 拼写错误的类或方法名称。 请记住, Java区分大小写,并且不会为您纠正拼写错误。 同样,下划线可能是必需的,也可能不是必需的,因此在不应该使用下划线时要格外小心,反之亦然。
  • The parameters used do not match a method's signature. 使用的参数与方法的签名不匹配。
  • The packaged class has not been referenced correctly using an import declaration.
  • Identifiers look the same but are actually different. This problem can be hard to spot, but in this case, if the source files use UTF-8 encoding, you may be using some identifiers as if they're identical but really they're not because they simply appear to be spelled the same. 标识符看起来相同,但实际上不同。 这个问题可能很难发现,但是在这种情况下,如果源文件使用UTF-8编码 ,则您可能会使用一些标识符,好像它们是相同的,但实际上并非如此,因为它们看起来只是拼写相同。
  • You're looking at the wrong source code. It may seem hard to believe that you're reading a different source code than the one producing the error, but it's definitely possible, and especially for new Java programmers. Check file names and version histories carefully.
  • You forgot a new, like this:  您忘了一个新的,像这样:
String s = String();
String s = new String();

Sometimes, the error arises from a combination of problems. Therefore, if you fix one thing, and the error persists, check for different problems still affecting your code.

有时,错误是由多种问题引起的。 因此,如果您修复了一件事情,并且错误仍然存在,请检查仍然影响您代码的其他问题。

For example, it's possible that you are trying to use an undeclared variable and when you fix it, the code still contains spelling errors.

例如,您可能尝试使用未声明的变量,并且在修复它时,代码仍包含拼写错误。

( Example of a "Cannot Find Symbol" Java Error )

Let's use this code as an example:

让我们以以下代码为例:

This code will cause a

此代码将导致

cannot find symbol

error because the

错误,因为

System.out

class does not have a method called “prontln”:

类没有名为“ prontln”的方法:

The two lines below the message will explain exactly what part of the code is confusing the compiler.

消息下方的两行将准确说明代码的哪一部分使编译器感到困惑。

Mistakes like capitalization mismatches are often flagged in a dedicated integrated development environment. Although you can write your Java code in any text editor, using IDEs and their associated linting tools reduces typos and mismatches. Common Java IDEs include Eclipse and NetBeans.

通常在专用的集成开发环境中标记出诸如大小写不匹配之类的错误。 尽管您可以在任何文本编辑器中编写Java代码,但使用IDE及其关联的整理工具可以减少输入错误和不匹配。 常见的Java IDE包括Eclipse和NetBeans。

翻译自: https://www.thoughtco.com/error-message-cannot-find-symbol-2034060

java 错误找不到符号