How To Do

[Fixed] How to Resolve “Cannot Find Symbol” Error in Java

Java is a general-purpose, object-oriented, strongly typed programming language for constructing net, cellular, and enterprise functions. Java, developed by Sun Microsystems and presently owned by Oracle Corporation, has turn into a highly regarded programming language due to its portability, scalability, reliability, and stability.

When writing applications utilizing Java, Java programming language is that Java the supply code is first compiled into bytecode. The bytecode is then interpreted by the Java Virtual Machine into machine code, thus permitting the Java program to run. Therefore, Java is each a compiled and interpreted language.

Compilation entails scanning a whole supply code and translating it into machine code directly. An interpreter, however, converts supply code into machine code one line at a time.

Young female programmer working in office

During compilation, the compiler creates an inventory of all identifiers used in this system. In Java, identifiers are names given to the completely different parts in this system, reminiscent of variables, lessons, strategies, packages, and interfaces, amongst others.

When a Java program is being compiled, errors might come up through the compilation course of. Such errors are generally known as compilation errors or compile-time errors. Compilation errors stop the Java compiler from efficiently producing a program’s bytecode.

Compilation errors stop your program from being compiled. Such errors could also be attributable to syntax, semantic or structural errors in the supply code. An instance of a compilation error is the Cannot Find Symbol Error. 

error: can not discover image

As famous earlier, throughout compilation, the Java compiler creates an inventory of all identifiers getting used in this system, and it figures out what every identifier means. If the compiler finds an identifier it doesn’t acknowledge or it may’t inform what the identifier refers to, a can not discover image error happens.

A can not discover image error is the compiler’s method of telling you that you’re utilizing an identifier that it can not perceive and thus can not determine what the identifier is meant to do.

This may very well be as a result of the identifier you are attempting to use has not been declared, shouldn’t be obtainable throughout the scope you’re utilizing it, or has not been imported appropriately. A can not discover image error can lead to failed compilation.

Causes of the error: can not discover image

Common-Cause-For-Error-0x80004005-

Some of the potential causes of the error: can not discover image embrace:

  • Typographical errors lead to misspelled variables, strategies, lessons, or bundle names. Using the incorrect case additionally leads to this error, as Java is case-sensitive.
  • Using a variable, methodology, or class that has not been declared or utilizing it earlier than its precise declaration.
  • Using variables, strategies, or lessons outdoors of the scope they’ve been declared in.
  • Creating an occasion of a category with out utilizing the brand new key phrase
  • Missing import statements when making an attempt to use lessons from different packages. Failure to import a category that’s in one other bundle earlier than utilizing it’s going to trigger this error too.
  • Incorrect bundle import.

When the compiler detects any of the above errors in your code, it’s going to cease compilation and throw the can not discover image error. 

Examples of error: can not discover image

Let us take an in depth have a look at code samples exhibiting the completely different causes of the can not discover image error and the way to resolve the error.

#1. Typographical Mistakes

Misspelled variables, strategies, lessons, or bundle names and incorrect circumstances lead to the can not discover image error. Consider the code under:

class Test {
  public static void fundamental(String[] args) {
    int even_number = 4;
    System.out.println(even_numbe); // misspelled variable identify
    System.out.println(Even_number);// incorrect case
    int sum = addnumber(5, 20); // misspelled methodology identify, incorrect case
    System.out.println(sum);
  }
  
  public static int addNumbers(int a, int b) {
    int consequence = a + b;
    return consequence;
  }
}

The results of compiling the code above is proven under:

Typographical-Mistakes

To appropriate the errors, ensure you are utilizing the right methodology and variable names. In the instance, the variable even_number and the tactic identify addNumbers had been misspelled they usually used the incorrect circumstances. To appropriate these errors, use the right variable and methodology identify that’s even_number, and addNumbers as proven under:

class Test {
  public static void fundamental(String[] args) {
    int even_number = 4;
    System.out.println(even_number); // Correct variable identify with the suitable case
    System.out.println(even_number); // Correct variable identify with the suitable case
    int sum = addNumbers(5, 20); // Correct methodology identify
    System.out.println(sum);
  }

  public static int addNumbers(int a, int b) {
    int consequence = a + b;
    return consequence;
  }
}

Output:

4
4
25
Corrected-Typos

#2. Undeclared Variable

Undeclared Variables, utilizing variables earlier than the declaration, and utilizing variables, strategies, and lessons outdoors their scope consequence in can not discover image as proven under:

class Test {
  public static void fundamental(String[] args) {
    System.out.println(y); // y is undeclared
    System.out.println(num); // num used earlier than its declaration
    int num = 4; // num's declaration and initialization
    if (num > 3) {
      int x = 2;
    };
    System.out.println(x); // x - used outdoors its scope
  }
}

The results of compiling this code is proven under:

Undeclared-variable

To appropriate this error, make sure that to declare each variable you utilize in your program and solely use them after declaring them. In our case, the variables y and num had been used earlier than being declared. Correct this error by utilizing them after their declaration.

The variable x was used outdoors the scope it was declared in. To appropriate this, use it throughout the if block the place it has been declared as proven under:

class Test {
  public static void fundamental(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 throughout the scope of the if assertion
    };
  }
}

Output:

Hello World
4
2
Corrected-undeclared-variable

#3. Out-of-scope variables, strategies, and lessons

Using variables, strategies, and lessons outdoors of the scope they’ve been declared in will lead to a can not discover image as proven in the code under:

class Test {
  public static void fundamental(String[] args) {
    if (true) {
      int quantity = 10;
    }
    System.out.println(quantity); // 'quantity' is out of scope
  }
}

Compiling this code leads to the next error:

Out-of-scope

To appropriate the error, guarantee you’re utilizing the variable quantity throughout the scope it has been declared. That is throughout the scope of the if block as proven under:

class Test {
  public static void fundamental(String[] args) {
    if (true) {
      int quantity = 10;
      System.out.println(quantity); // quantity - used throughout the scope of the if block
    }
  }
}

Output:

10
Corrected-Scope

#4. Creating an occasion of a category with out utilizing the brand new key phrase

Java is an object-oriented language and also you thus can create objects often known as class cases in a Java program. However, when you do that with out utilizing the new key phrase, you’ll run right into a can not discover image as proven under:

public class Car {
  non-public String make;
  non-public String mannequin;

  public Car(String make, String mannequin) {
    this.make = make;
    this.mannequin = mannequin;
  }

  public String getMake() {
    return make;
  }

  public String getModel() {
    return mannequin;
  }

  public static void fundamental(String[] args) {
    // Creating an occasion of the Car class with out utilizing new
    Car myCar = Car("Ford", "Fiesta");

    System.out.println("Make: " + myCar.getMake());
    System.out.println("Model: " + myCar.getModel());
  }
}

Compiling the code outcomes in an error as proven under:

No-new-keyword

To appropriate the error, use the new key phrase at any time when you’re creating objects. To create an occasion of a category in Java, you want to use the brand new key phrase. To create an occasion of the category Car you thus have to use new Car() as proven under:

public class Car {
  non-public String make;
  non-public String mannequin;

  public Car(String make, String mannequin) {
    this.make = make;
    this.mannequin = mannequin;
  }

  public String getMake() {
    return make;
  }

  public String getModel() {
    return mannequin;
  }

  public static void fundamental(String[] args) {
    // Instance of Car created appropriately by utilizing the brand new key phrase
    Car myCar = new Car("Ford", "Fiesta");

    System.out.println("Make: " + myCar.getMake());
    System.out.println("Model: " + myCar.getModel());
  }
}

Output:

Make: Ford
Model: Fiesta
corrected-no-new-keyword

#5. Missing Import

Trying to use a category discovered in one other bundle with out importing it leads to the can not discover image not error. See the code under:

class Test {
  public static void fundamental(String[] args) {
    System.out.println("Hello, enter any number");
    // Missing import for the Scanner class
    Scanner scanner = new Scanner(System.in);
    int quantity = scanner.subsequentInt();
    System.out.println("The number you entered is: " + quantity);
    scanner.shut();
  }
}

The results of compiling the code is proven under:

Missing-Import

The Scanner class is used to get person enter from the keyboard in a Java program. To use it, you want to first import it into the file you need to use it. Importing the scanner class makes it obtainable to be used as proven. The import assertion is the primary line in the code under:

import java.util.Scanner;
class Test {
  public static void fundamental(String[] args) {
    System.out.println("Hello, enter any number");
    // Missing import for the Scanner class
    Scanner scanner = new Scanner(System.in);
    int quantity = scanner.subsequentInt();
    System.out.println("The number you entered is: " + quantity);
    scanner.shut();
  }
}

Output:

Hello, enter any quantity
45
The quantity you entered is: 45
Corrected-Missing-Import

#6. Incorrect Import

Aside from the failure to import a bundle you need to use, importing the incorrect bundle can even lead to a can not discover image. Consider the code under:

import java.util.Dates; // importing the incorrect bundle - appropriate bundle is Date
class Test {
  public static void fundamental(String[] args) {
    Dates now = new Dates();
        System.out.println(now);
  }
}

Compiling the code outcomes in the next errors:

Incorrect-Import

To appropriate the can not discover image errors generate, guarantee you’re importing the suitable bundle. In the code that generated an error, the category that was imported was Dates. However, there isn’t a inbuilt class known as Dates. The appropriate identify is Date. Therefore, to take away the error, import Date and never Dates as proven under:

import java.util.Date; // importing the right bundle - Date and never Dates
class Test {
  public static void fundamental(String[] args) {
    Date now = new Date();
        System.out.println(now);
  }
}

Output:

Thu Aug 17 12:02:12 EAT 2023
Corrected-Incorrect-Import

Impact of error: can not discover the image in Compilation

Being a compilation error, essentially the most speedy impression of the can not discover image error is a failure in the compilation course of. When Java is being compiled to bytecode, errors detected in the supply code consequence in the failure of the compilation course of. Therefore, no bytecode shall be generated to be used in executing this system.

When such errors come up, the plain reason behind motion is to begin debugging your supply code to discover what’s inflicting the errors and proper the errors. Whereas debugging can take pleasure in serving to you become familiar higher with a language, it may lead to growth delays. Additionally, the general supply code high quality is affected due to bugs in the supply code.

Conclusion

They can not discover image errors, a quite common error, and you’re sure to encounter them whereas writing Java code. Luckily, additionally it is a very simple error to keep away from. Cannot discover image error outcomes from the incorrect use of identifiers. Therefore to keep away from the error, make sure you declare all of your variables, strategies, and lessons earlier than utilizing them.

Additionally, ensure you don’t make any typos when referencing the identifiers you’re utilizing in your program, and use all of your identifiers throughout the scope you’ve declared them. Finally, in case you’ll be utilizing any bundle or exterior lessons in your program, make sure that to import the suitable packages into your program earlier than you begin utilizing them.

You can also discover the perfect Java monitoring software program for companies.

Vikash Gorayan

Vikash Gorayan is a technology enthusiast with a passion for gadgets and product management. He is constantly seeking new opportunities to learn and grow in his field.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button