Open In App

How to Resolve The Cannot Find Symbol Error in Java?

Last Updated : 14 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The Cannot Find Symbol Error in Java error occurs when the Java compiler cannot find a symbol that you are trying to reference in your code. In this article, we will explore how to resolve the “Cannot Find Symbol” error in Java.

The “Cannot Find Symbol” error occurs when you are trying to reference a symbol that has not been defined or imported properly. Symbols can include variables, methods, and classes or in easy language when you try to reference an undeclared variable in your code. The error typically occurs when you have made a typo, used the wrong case in the symbol name, or when you are trying to reference a symbol that is out of scope. You may also encounter this error when you are using multiple files, and the compiler cannot find a class or package that you are trying to reference.

Common mistakes that lead to “Cannot Find Symbol” error in Java and resolving them, you can follow these steps:

  • Typos
  • Undeclared Variable
  • Scope of Current block
  • Import Statement

Typos

Make sure that the symbol you are trying to reference is spelled correctly and matches the case used in the definition.

Example 1:

Java




// Java Program to demonstrate typos
public class Main {
    static int Large(int a, int b)
    {
        if (a > b) {
            return a;
        }
        else if (b > a) {
            return b;
        }
        else {
            return -1;
        }
    }
  
    public static void main(String... args)
    {
        int value = large(20, 4);
        System.out.println(value);
    }
}


Output:

./Main.java:17: error: cannot find symbol
        int value = large(20, 4);
                    ^
  symbol:   method large(int,int)
  location: class Main

Explanation of the above program

We get this error as the function we initialized is Large(int a,int b) whereas we are calling large(a,b) so to resolve this typo mistake we just change large to Large.

Example 2:

Java




// Java Program to print largest number
public class Main {
    static int Large(int a, int b)
    {
        if (a > b) {
            return a;
        }
        else if (b > a) {
            return b;
        }
        else {
            return -1;
        }
    }
  
    public static void main(String... args)
    {
        // function call
        int value = Large(20, 4);
        System.out.println(value);
    }
}


Output

20

Undeclared Variable

We can also get errors by undeclared Variables.

Example 1:

Java




// Java Program to demonstrate use of undeclared variable
public class Main {
    public static void main(String[] args)
    {
        int n1 = 500;
        int n2 = 400;
        sum = n1 + n2;
        System.out.println(sum);
    }
}


Output: 

./Main.java:6: error: cannot find symbol
        sum = n1 + n2;
        ^
  symbol:   variable sum
  location: class Main
./Main.java:7: error: cannot find symbol
        System.out.println(sum);
                           ^
  symbol:   variable sum
  location: class Main

Explanation of the program

In the above example, the “Cannot find symbol” error occurs as we have not declared the sum. The corrected code is given below (will occur because “sum” is not declared. In order to solve the error, we need to define “int sum = n1+n2” before using the variable sum.

Example 2:

Java




// Java Program to pint sum of two numbers
public class Main {
    public static void main(String[] args)
    {
        int n1 = 500;
        int n2 = 400;
        int sum = n1 + n2;
        System.out.println(sum);
    }
}


Output

900

Check the scope

Make sure that the symbol you are trying to reference is within the scope of the current code block.

Example 1:

Java




// Java Program to demonstrate scope of code
public class Main {
    public static void main(String[] args)
    {
        int x = 5;
        if (x > 0) {
            int y = 10;
        }
        // error: cannot find symbol
        System.out.println(y);
    }
}


Output:

./Main.java:8: error: cannot find symbol
                System.out.println(y);
                                   ^
  symbol:   variable y
  location: class Main

Explanation of the above program

In this program, we define an integer variable x and initialize it to 5. We then have an if statement that checks if x is greater than 0. If it is, we define another integer variable y, and initialize it to 10. However, y is only within the scope of the if block, and cannot be accessed outside of it. 

Example 2:

Java




public class Main {
    public static void main(String[] args) {
        int x = 5;
        int y = 0;
        if (x > 0) {
            y = 10;
        }
        System.out.println(y);
    }
}


Output

10

Check the import statements

Make sure that any classes or packages that you are trying to reference are imported properly.

Example 1:

Java




// java program to demonstrate wrong use of import
// statements
import java.util.Rand;
  
public class Main {
    public static void main(String[] args)
    {
        Rand rand = new Rand();
        int x = rand.nextInt(10);
        System.out.println("Random number: " + x);
    }
}


Output:

./Main.java:1: error: cannot find symbol
import java.util.Rand;
                ^
  symbol:   class Rand
  location: package java.util
./Main.java:6: error: cannot find symbol
        Rand rand = new Rand();
        ^
  symbol:   class Rand
  location: class Main
./Main.java:6: error: cannot find symbol
        Rand rand = new Rand();
                        ^
  symbol:   class Rand
  location: class Main

Example 2:

Java




import java.util.Random;
  
public class Main {
    public static void main(String[] args) {
        Random rand = new Random();
        int x = rand.nextInt(10);
        System.out.println("Random number: " + x);
    }
}


Output

Random number: 3


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads