Java is a general-purpose, object-oriented, strongly typed programming language for building web, mobile, and enterprise applications. Java, developed by Sun Microsystems and currently owned by Oracle Corporation, has become a very popular programming language because of its portability, scalability, reliability, and stability.
When writing programs using Java, Java programming language is that Java the source code is first compiled into bytecode. The bytecode is then interpreted by the Java Virtual Machine into machine code, thus allowing the Java program to run. Therefore, Java is both a compiled and interpreted language.
Compilation involves scanning an entire source code and translating it into machine code at once. An interpreter, on the other hand, converts source code into machine code one line at a time.
During compilation, the compiler creates a list of all identifiers used in the program. In Java, identifiers are names given to the different elements in the program, such as variables, classes, methods, packages, and interfaces, among others.
When a Java program is being compiled, errors may arise during the compilation process. Such errors are known as compilation errors or compile-time errors. Compilation errors prevent the Java compiler from successfully generating a program’s bytecode.
Compilation errors prevent your program from being compiled. Such errors may be caused by syntax, semantic or structural errors in the source code. An example of a compilation error is the Cannot Find Symbol Error.
error: cannot find symbol
As noted earlier, during compilation, the Java compiler creates a list of all identifiers being used in the program, and it figures out what each identifier means. If the compiler finds an identifier it does not recognize or it can’t tell what the identifier refers to, a cannot find symbol error occurs.
A cannot find symbol error is the compiler’s way of telling you that you are using an identifier that it cannot understand and thus cannot figure out what the identifier is supposed to do.
This could be because the identifier you are trying to use has not been declared, is not available within the scope you are using it, or has not been imported correctly. A cannot find symbol error can lead to failed compilation.
Causes of the error: cannot find symbol
Some of the potential causes of the error: cannot find symbol include:
- Typographical mistakes lead to misspelled variables, methods, classes, or package names. Using the wrong case also leads to this error, as Java is case-sensitive.
- Using a variable, method, or class that has not been declared or using it before its actual declaration.
- Using variables, methods, or classes outside of the scope they have been declared in.
- Creating an instance of a class without using the new keyword
- Missing import statements when trying to use classes from other packages. Failure to import a class that is in another package before using it will cause this error too.
- Incorrect package import.
When the compiler detects any of the above mistakes in your code, it will stop compilation and throw the cannot find symbol error.
Examples of error: cannot find symbol
Let us take a detailed look at code samples showing the different causes of the cannot find symbol error and how to resolve the error.
#1. Typographical Mistakes
Misspelled variables, methods, classes, or package names and wrong cases lead to the cannot find symbol error. Consider the code below:
class Test {
public static void main(String[] args) {
int even_number = 4;
System.out.println(even_numbe); // misspelled variable name
System.out.println(Even_number);// wrong case
int sum = addnumber(5, 20); // misspelled method name, wrong case
System.out.println(sum);
}
public static int addNumbers(int a, int b) {
int result = a + b;
return result;
}
}
The result of compiling the code above is shown below:
To correct the errors, make sure you are using the correct method and variable names. In the example, the variable even_number and the method name addNumbers were misspelled and they used the wrong cases. To correct these errors, use the correct variable and method name that is even_number, and addNumbers as shown below:
class Test {
public static void main(String[] args) {
int even_number = 4;
System.out.println(even_number); // Correct variable name with the right case
System.out.println(even_number); // Correct variable name with the right case
int sum = addNumbers(5, 20); // Correct method name
System.out.println(sum);
}
public static int addNumbers(int a, int b) {
int result = a + b;
return result;
}
}
Output:
4
4
25
#2. Undeclared Variable
Undeclared Variables, using variables before the declaration, and using variables, methods, and classes outside their scope result in cannot find symbol as shown below:
class Test {
public static void main(String[] args) {
System.out.println(y); // y is undeclared
System.out.println(num); // num used before its declaration
int num = 4; // num's declaration and initialization
if (num > 3) {
int x = 2;
};
System.out.println(x); // x - used outside its scope
}
}
The result of compiling this code is shown below:
To correct this error, make sure to declare every variable you use in your program and only use them after declaring them. In our case, the variables y and num were used before being declared. Correct this error by using them after their declaration.
The variable x was used outside the scope it was declared in. To correct this, use it within the if block where it has been declared as shown below:
class Test {
public static void main(String[] args) {
String y = "Hello World";
System.out.println(y); // y - used after it has been declared
int num = 4;
System.out.println(num); // num - used after it has been declared
if (num > 3) {
int x = 2;
System.out.println(x); // x - used within the scope of the if statement
};
}
}
Output:
Hello World
4
2
#3. Out-of-scope variables, methods, and classes
Using variables, methods, and classes outside of the scope they have been declared in will lead to a cannot find symbol as shown in the code below:
class Test {
public static void main(String[] args) {
if (true) {
int number = 10;
}
System.out.println(number); // 'number' is out of scope
}
}
Compiling this code leads to the following error:
To correct the error, ensure you are using the variable number within the scope it has been declared. That is within the scope of the if block as shown below:
class Test {
public static void main(String[] args) {
if (true) {
int number = 10;
System.out.println(number); // number - used within the scope of the if block
}
}
}
Output:
10
#4. Creating an instance of a class without using the new keyword
Java is an object-oriented language and you thus can create objects also known as class instances in a Java program. However, if you do this without using the new keyword, you’ll run into a cannot find symbol as shown below:
public class Car {
private String make;
private String model;
public Car(String make, String model) {
this.make = make;
this.model = model;
}
public String getMake() {
return make;
}
public String getModel() {
return model;
}
public static void main(String[] args) {
// Creating an instance of the Car class without using new
Car myCar = Car("Ford", "Fiesta");
System.out.println("Make: " + myCar.getMake());
System.out.println("Model: " + myCar.getModel());
}
}
Compiling the code results in an error as shown below:
To correct the error, use the new keyword whenever you are creating objects. To create an instance of a class in Java, you need to use the new keyword. To create an instance of the class Car you thus have to use new Car() as shown below:
public class Car {
private String make;
private String model;
public Car(String make, String model) {
this.make = make;
this.model = model;
}
public String getMake() {
return make;
}
public String getModel() {
return model;
}
public static void main(String[] args) {
// Instance of Car created correctly by using the new keyword
Car myCar = new Car("Ford", "Fiesta");
System.out.println("Make: " + myCar.getMake());
System.out.println("Model: " + myCar.getModel());
}
}
Output:
Make: Ford
Model: Fiesta
#5. Missing Import
Trying to use a class found in another package without importing it leads to the cannot find symbol not error. See the code below:
class Test {
public static void main(String[] args) {
System.out.println("Hello, enter any number");
// Missing import for the Scanner class
Scanner scanner = new Scanner(System.in);
int number = scanner.nextInt();
System.out.println("The number you entered is: " + number);
scanner.close();
}
}
The result of compiling the code is shown below:
The Scanner class is used to get user input from the keyboard in a Java program. To use it, you need to first import it into the file you want to use it. Importing the scanner class makes it available for use as shown. The import statement is the first line in the code below:
import java.util.Scanner;
class Test {
public static void main(String[] args) {
System.out.println("Hello, enter any number");
// Missing import for the Scanner class
Scanner scanner = new Scanner(System.in);
int number = scanner.nextInt();
System.out.println("The number you entered is: " + number);
scanner.close();
}
}
Output:
Hello, enter any number
45
The number you entered is: 45
#6. Incorrect Import
Aside from the failure to import a package you want to use, importing the wrong package can also lead to a cannot find symbol. Consider the code below:
import java.util.Dates; // importing the wrong package - correct package is Date
class Test {
public static void main(String[] args) {
Dates now = new Dates();
System.out.println(now);
}
}
Compiling the code results in the following errors:
To correct the cannot find symbol errors generate, ensure you are importing the right package. In the code that generated an error, the class that was imported was Dates. However, there is no inbuilt class called Dates. The correct name is Date. Therefore, to remove the error, import Date and not Dates as shown below:
import java.util.Date; // importing the correct package - Date and not Dates
class Test {
public static void main(String[] args) {
Date now = new Date();
System.out.println(now);
}
}
Output:
Thu Aug 17 12:02:12 EAT 2023
Impact of error: cannot find the symbol in Compilation
Being a compilation error, the most immediate impact of the cannot find symbol error is a failure in the compilation process. When Java is being compiled to bytecode, errors detected in the source code result in the failure of the compilation process. Therefore, no bytecode will be generated to be used in executing the program.
When such errors arise, the obvious cause of action is to start debugging your source code to find what is causing the errors and correct the errors. Whereas debugging can have the benefit of helping you familiarize yourself better with a language, it can lead to development delays. Additionally, the overall source code quality is affected due to bugs in the source code.
Conclusion
They cannot find symbol errors, a very common error, and you are bound to encounter them while writing Java code. Luckily, it is also a very easy error to avoid. Cannot find symbol error results from the wrong use of identifiers. Therefore to avoid the error, ensure you declare all your variables, methods, and classes before using them.
Additionally, make sure you don’t make any typos when referencing the identifiers you are using in your program, and use all your identifiers within the scope you’ve declared them. Finally, in case you’ll be using any package or external classes in your program, make sure to import the right packages into your program before you start using them.
You may also explore the best Java monitoring software for businesses.
-
EditorNarendra Mohan Mittal is a senior editor at Geekflare. He is an experienced content manager with extensive experience in digital branding strategies.