Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
How to Solve java.lang.ClassNotFoundException in Java?
Next article icon

How to Resolve The Cannot Find Symbol Error in Java?

Last Updated : 14 May, 2023
Comments
Improve
Suggest changes
Like Article
Like
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

Next Article
How to Solve java.lang.ClassNotFoundException in Java?

H

hashharsh13
Improve
Article Tags :
  • Java
  • Java Errors
Practice Tags :
  • Java

Similar Reads

  • How to Solve Class Cast Exceptions in Java?
    An unexcepted, unwanted event that disturbed the normal flow of a program is called Exception. Most of the time exceptions are caused by our program and these are recoverable. Example: If our program requirement is to read data from the remote file locating at the U.S.A. At runtime, if the remote fi
    3 min read
  • How to Fix int cannot be dereferenced Error in Java?
    Here, we will follow the below-mentioned points to understand and eradicate the error alongside checking the outputs with minor tweaks in our sample code Introduction about the error with exampleExplanation of dereferencing in detailFinally, how to fix the issue with Example code and output. If You
    4 min read
  • How to Solve java.lang.ClassNotFoundException in Java?
    In Java, java.lang.ClassNotFoundException is a checked exception and occurs when the Java Virtual Machine (JVM) tries to load a particular class and the specified class cannot be found in the classpath. ClassNotFoundException should be handled with a try-catch block or using the throw keyword. In ol
    4 min read
  • How to Set Java SDK Path in Android Studio?
    The Java SDK for Android is a sophisticated suite of tools for managing, monitoring, profiling, and debugging Java code written in Android Studio. But sometimes as software is unpredictable you might be caught in an error that Android Studio stopped compiling projects and says that it can't locate t
    4 min read
  • How to Resolve Java.lang.ExceptionInInitializerError In Java?
    An unexpected, unwanted event that disturbed the normal flow of a program is called an Exception. There are mainly two types of exception in Java: 1. Checked Exception 2. Unchecked Exception ExceptionInInitializerError is the child class of the Error class and hence it is an unchecked exception. Thi
    3 min read
  • How to Calculate Size of Object in Java?
    In Java, an object is an instance of a class that encapsulates data and behavior. Calculating the size of an object can be essential for memory management and optimization. This process involves understanding the memory layout of the object, including its fields and the overhead introduced by the JV
    2 min read
  • How to Debug a Java Project in Eclipse?
    Every Java developer needs to work in Debug mode to resolve the errors. Experienced Java developer already knows how to debug the code but if you are new as a Java developer this article will cover the topics which help to debug the code faster in eclipse and also use the "Debug Mode" efficiently an
    3 min read
  • How to Fix java.lang.classcastexception in Java?
    ClassCastException in Java occurs when we try to convert the data type of entry into another. This is related to the type conversion feature and data type conversion is successful only where a class extends a parent class and the child class is cast to its parent class. Here we can consider parent c
    6 min read
  • How to Create Jar File for Java Project in Eclipse?
    Java programmers already know about what is a jar file if you are new then first understand what is a jar file. Once you are completed your project in Eclipse the next task is to create a runnable version of your project. Eclipse support only exporting the JAR (.jar) file, not the Executable (.exe)
    3 min read
  • Unreachable Code Error in Java
    The Unreachable statements refers to statements that won’t get executed during the execution of the program are called Unreachable Statements. These statements might be unreachable because of the following reasons: Have a return statement before them Have an infinite loop before them Any statements
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences