. Advertisement .
..3..
. Advertisement .
..4..
There are many errors related to variables when programming the language in Java. The message cannot be resolved to a variable is a popular mistake you could see. What causes this error message, and how to correct it? Continue reading the below for more info!
Fix The Error Cannot Be Resolved To A Variable In Java
It is necessary to understand what is the scope of the variable used to program the language. Generally, it is known as a part of the program where the variable is valid or accessible. Scope of variable is defined by a block that begins with opening curly braces { and finishes with a closing bracket }.
Variables run successfully without any problems with their own scope. Due to this feature, the error comes out differently for each variable.
Let’s learn about the cause and solution of each variable in the section below.
Error Of Local Variables
A local variable can be declared everywhere in the program but inside a block, constructor, and method. Therefore, its scope remains only within that block, constructor, and method. If you try to call a local variable out of its scope, the problem that cannot be resolved will pop up. So, do not access these types of variables from outside their scope, which are in curly brackets.
For example, take a look below for more understanding.
package variablePrograms;
public class School
{
// Declare instance variables.
public String name = "John";
// Declaration of constructor.
School()
{
int id = 999;
System.out.println("Id of Student: " +id);
}
// Declaration of user-defined method in instance area.
public void mySchool()
{
// Declaration of local variables.
String schoolName = "AAA";
System.out.println("Name of School: " +schoolName);
}
public void mySchool1()
{
System.out.println("Name of School: " +schoolName)// Not possible because local variables cannot be accessed from outside the method, constructor, or block.
}
public static void main(String[] args)
{
// Create the object of class 'School'.
School sc = new School();
sc.mySchool();
}
}
The output is:
Id of student: 999
Name of School: AAA
In the above code, there is the scope limitation of the local variable schoolName which is at the curly brackets. Moreover, we also have mySchool() method. So you cannot access this variable outside this method as well as its scope. If you try to do so, the variable cannot be resolved error will show off.
Error Of Private Access Variables
Similar to the previous variables, private variables can be accessed outside the block, constructor, and method. Then, the scope of these variables is accessible outside that block, constructor, and method.
To avoid the error that cannot be resolved to a variable, you must call it outside their block or constructor. In other words, you cannot access it inside a constructor.
Find the self-explanatory program as follows:
public class Main
{
public static void main(String args[])
{
int var =3;
// scope is a limited within main Block;
// The Scope of var Amount Is Limited..........
// Accessible only Within this block............
}
public static void Calculate (int amount)
{
// The Scope of Variable Amount Is Limited..........
// Accessible only Within this block............
}
}
Conclusion
The cannot be resolved to a variable is one of the most common errors in Java programming language. Each variable has its own feature and scope limitation. Having a deep understanding of their scope is the best way to avoid the above problems. Hopefully, our blog is helpful for your preference.
Leave a comment