What is the meaning of the compilation error "Cannot find symbol"?

Asked 2 years ago, Updated 2 years ago, 126 views

Please explain a few things about the "Cannot find symbol" error.

java compiler-errors cannot-find-symbol

2022-09-22 22:02

1 Answers

First, this is a compilation error. It could be a problem within the Java source code you wrote, or it could be a problem with the compilation method.

The Java source code consists of the following::

"Cannot find symbol" is an error for the identifier. When the compiler compiles the source code, the compiler must know what each identifier means in the source code.

"Cannot find symbol" indicates that the compiler does not understand the meaning of the identifier. In other words, the source code is judged to have used an identifier that the compiler does not understand.

There is only one reason for this error. The compiler looks for where the identifier is declared. However, this error occurs if the declaration is not found. This can happen in many cases.

About identifiers:

For identifiers referring to variables:

About method names:

About class names:

String s = String(); // should be 'new String()'

These problems can occur in combination. For example, if you import the java.io.* package using "star" and try to use the Files class defined in the java.nio package instead of the java.io package. Alternatively, this can also happen if you try to use the File class in the java.io package, but accidentally use the Files class.

The following is an example in which a "Cannot find symbol" error may occur when the variable is out of service.

for( int i = 0; i < strings.size(); i++) {
   if(strings.get(i).equalsIgnoreCase("fnoord")) {
      break;
   }
}
if(i < strings.size()) {
   ...
}

The error "Cannot find symbol" will occur for i in the if statement. This is because i declared before the if statement is declared within the for statement and can only be used within the repeat statement. Therefore, i in if statement cannot see the declaration of i. In this case, we call it "out of range of use of variables."

(To solve this problem, you can define the if statement within the repeat statement, or declare i before the repeat statement begins.)

If the terminal compiles to a command line, there may be another reason why the compiler cannot find the symbol. Failure to compile certain classes simply can cause problems. For example, if you have a Foo class that uses the Bar class and the Bar class, the Bar class does not compile, and only the Java Foo.jav> class does not have a compilation. To solve this problem, both Foo and Bar classes need to be compiled. java Foo.java Bar.Like java or javac *.java. Therefore, it is better to use Java compilation tools such as Ant, Maven, and Gradle than to compile directly with the command line.

In general, you should start by understanding what is causing the problem. Then you have to think about what the source code is trying to implement, and finally modify it to fulfill what it wants to implement.

For example:

for(int i = 1; i < 10; i++) {
   for(j = 1; j < 10; j++) {

} }


You can expect the compiler to generate a "Cannot find symbol" error for 'j'. There can be many ways to solve this problem.

* The inner 'for' statement can be modified as 'for(int j = 1; j < 10; j++)'.
* You can declare 'j' before the 'for' door inside, or 'j' before the 'for' door outside. 
* You can also change the 'j' of the inner 'for' statement to 'i'. However, this may have unintended consequences.
* And so on

### 4. Ambiguous causes

The location of the "Cannot find symbol" error may be ambiguous.

1. *If you are using an invalid source code*: 
New Java developers often develop without understanding how Java development tools (IDE, Ant, Maven Gradle, etc.) work. In this case, the programmer may have trouble correcting errors on the source code, such as not recompiling the source code.

2. Override system classes:
In some cases, the compiler may generate an error called unknown symbol for the 'substring' method:

String s = ... String s1 = s.substring(1);


If the developer defines a class named 'String' and the method 'substring' is not defined in the class, the name may overlap with Java's 'String' class and cause an error.

We recommend that you do not create classes with the same name as those defined in a typical library!

3. Homoglyphs

If the source file is encoded in UTF-8, there may be cases where different characters are actually recognized as the same characters because of homoglyphs. 
You can use the '\uxxx' character to avoid encoding the source file as ASCII or Latin-1.


2022-09-22 22:02

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.