Core Java

How to fix cannot find symbol Java Error

We would have come across the error – Cannot find symbol when compiling our Java classes. In this article, let us look at some common causes and how to avoid them.

1. Cannot find symbol Java error

This compilation error occurs when the compiler is unable to get an identifier declared in the code. Some of the usual causes could be:

cannot find symbol Java
  • identifier referenced with an incorrect spelling
  • identifier used with an incorrect case (as Java is case sensitive)
  • variable not declared or out of scope
  • package not imported for the class being referenced
  • the inherited method being referenced is not available in the parent class

Let us look at an example that highlights the issues mentioned above.

VariableDeclSample.java

package examples;

import examples.sample.SampleClass;

/*
* A class to simulate the error - Cannot find symbol
*/
public class VariableDeclSample {
	private String testVar;
	public void testVariables(){
		String sample="";
		// incorrect identifier used. testVar is not same as test_var
		test_var="def";
		
		// incorrect case used for identifier.
		testvar = "abc";
		
		SampleClass testClass = new SampleClass();
		// incorrect method being referenced
		testClass.samplemethod();
	}
	
	public void sampleMethod(){
		// variable sample is out of scope in this method
		sample = "test";
		
	}
}

SampleClass.java

package examples.sample;

public class SampleClass {
	
	public void sampleMethod(){
		
		System.out.println("in sampleMethod");
		
	}
}

The class VariableDeclSample displays all the common causes. Line 13 has the incorrect identifier. Line 16 has an incorrect case for identifier. Line 20 has an incorrect method reference. Line 25 uses the variable that is out of scope. Compiling the class would show errors as below.

.\examples\VariableDeclSample.java:13: error: cannot find symbol
                test_var="def";
                ^
  symbol:   variable test_var
  location: class VariableDeclSample
.\examples\VariableDeclSample.java:16: error: cannot find symbol
                testvar = "abc";
                ^
  symbol:   variable testvar
  location: class VariableDeclSample
.\examples\VariableDeclSample.java:20: error: cannot find symbol
                testClass.samplemethod();
                         ^
  symbol:   method samplemethod()
  location: variable testClass of type SampleClass
.\examples\VariableDeclSample.java:25: error: cannot find symbol
                sample = "test";
                ^
  symbol:   variable sample
  location: class VariableDeclSample
4 errors

2. How to fix these errors

Check the line in the file indicated by the error message and identify which symbol fails compilation and fix based on the above possibilities. Use proper naming conventions.

3. Download the source code

Download
You can download the full source code of this example here: How to fix cannot find symbol Java Error

Venkat-Raman Nagarajan

Venkat works for a major IT firm in India and has more than a decade of experience working and managing Java projects for a banking client.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Sukrati
Sukrati
3 years ago

I am geeting this error.. please tell me what is wrong in this.. Programme

Back to top button