I’ve noticed bizarre behavior with Eclipse version 4.2.1 delivering me this error:
String cannot be resolved to a variable
With this Java code:
if (true)
String my_variable = "somevalue";
System.out.println("foobar");
You would think this code is very straight forward, the conditional is true, we set my_variable to somevalue. And it should print foobar. Right?
Wrong, you get the above mentioned compile time error. Eclipse is trying to prevent you from making a mistake by assuming that both statements are within the if statement.
If you put braces around the conditional block like this:
if (true){
String my_variable = "somevalue"; }
System.out.println("foobar");
Then it compiles and runs fine. Apparently poorly bracketed conditionals are fair game for generating compile time errors now.
When a programmer writes a program in Java, every symbol is placed into the symbol tables, which are used subsequently in the compilation process. As class, variable, and method declarations are processed, their identifiers are bound to matching entries in the symbol tables. Symbol tables are important data structures compilers generate and maintain to hold information used in a source code.
When the Java compiler detects the use of an identifier-like variable that is not located in the symbol table, it encounters the “cannot be resolved” error.
This article will discuss the specified error and offer the method to solve it.
The missing variable declaration is the most common cause of this error. This error is frequently encountered due to referencing variables and methods outside of their scope or when a reference to an undeclared variable is specified.
As in the given code snippet, we will access variables “a” and “b” in the main() method while it is declared in the “sum()” method, so it throws a Java error “cannot be resolved to a variable” because the specified variables are inaccessible in the current scope. Therefore, a variable must be declared first within the scope before using it in Java:
Now, let’s check out the methods related to fixing the specified error.
How to fix the Java “cannot be resolved to a variable” Error?
To fix the mentioned error, you must use the variable inside the scope. Curly brackets are used in the “{ }” Java to indicate the scope of a class, functions, and various methods.
Here in the given example, we will declare variables “a” and “b” in the “sum()” method. So, the scope of these variables is inside the curly braces of the stated method:
public static int sum (int a, int b)
{
int add = a+b;
return add;
}
We will print the value of the sum of variables by calling the method “sum()” in the main() method by storing it in an integer type variable “s”:
public static void main(String[] args) {
int s = sum(2,3);
System.out.println(«Sum of two numbers = « + s);
}
In the given example, we have utilized the declared variables within their scope and accessed them without any hassle:
We have compiled all the basic information about what “cannot be resolved to a variable” error is and how to fix it.
Conclusion
To fix the Java “cannot be resolved to a variable” error, you must declare the variable inside the scope where you will utilize it. The missing variable declaration is the most common cause of this error. That’s why it is important to declare a variable before using it in the current scope. This article discussed the reasons for the specified error and how we will fix it.
About the author

I completed my master’s degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.
I’ve noticed bizarre behavior with Eclipse version 4.2.1 delivering me this error:
String cannot be resolved to a variable
With this Java code:
if (true)
String my_variable = "somevalue";
System.out.println("foobar");
You would think this code is very straight forward, the conditional is true, we set my_variable to somevalue. And it should print foobar. Right?
Wrong, you get the above mentioned compile time error. Eclipse is trying to prevent you from making a mistake by assuming that both statements are within the if statement.
If you put braces around the conditional block like this:
if (true){
String my_variable = "somevalue"; }
System.out.println("foobar");
Then it compiles and runs fine. Apparently poorly bracketed conditionals are fair game for generating compile time errors now.
This guide will teach you how to fix the cannot be resolved to a variable error in Java.
For this, you need to understand the scope of a programming language. Keep reading this compact guide to learn more and get your fix to this error.
Fix the cannot be resolved to a variable Error in Java
In Java programming language, we use curly brackets {} to identify the scope of a class, functions, and different methods.
For instance, take a look at the following code:
public static void calculateSquareArea(int x)
{
System.out.println(x*x);
}
In the above code example, the scope of the variable x is limited within the curly brackets {}. You cannot call or use it outside this scope. If you try, the cannot be resolved to a variable error will come out.
It means it cannot detect the initialization of variables within its scope. Similarly, if you make a private variable, you cannot call it inside a constructor.
Its scope is out of bounds. Here’s the self-explanatory code.
public class Main
{
public static void main(String args[])
{
int var =3;
// scope is 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............
}
}
Hi Everyone,
I’m getting this weird error and eclipse is telling me my finalAmount variable can’t be resolved. From looking at it I know it’s outside of the scoop of the for statement and everytime I create a local variable and assign a value to it (it’s zero) my program gets messed up.
My question is, how would I declare the finalAmount variable locally?
Thanks
import java.util.Scanner; public class CompoundInterest { public static void main(String[] args){ double rate; double amount; double year; System.out.println("This program, with user input, computes interest.\n" + "It allows for multiple computations.\n" + "User will input initial cost, interest rate and number of years."); Scanner input = new Scanner(System.in); System.out.println("What is the inital cost?"); amount = input.nextDouble(); System.out.println("What is the interest rate?"); rate = input.nextDouble(); rate = rate/100; System.out.println("How many years?"); year = input.nextDouble(); for(int x = 1; x < year; x++){ double finalAmount = amount * Math.pow(1.0 + rate, year); // the below works but the problem is, it prints the statement out many times. I don't want that. /* System.out.println("For " + year + " years an initial " + amount + " cost compounded at a rate of " + rate + " will grow to " + finalAmount); */ } System.out.println("For " + year + " years an initial " + amount + " cost compounded at a rate of " + rate + " will grow to " + finalAmount); } }
1 Answer
Shane,
Q: «eclipse is telling me my finalAmount variable can’t be resolved»
A: This is because you are declaring ‘finalAmount’ within the for loop. Once your for loop exits, ‘finalAmount’ goes out of scope. Meaning, Java has no clue it ever existed.
Q: «My question is, how would I declare the finalAmount variable locally?»
A: From what I know, you cannot declare a variable within a loop of any kind if you want to retain the previous value. When you declare a variable within a loop this is what happens:
- Your loop begins with an initial value of 0. (This is before the calculation takes place, double finalAmount;)
- A value is calculated and assigned to finalAmount.
- Your loop ends.
- If you loop condition is still valid (x < year), repeat from step one (finalAmount is redeclared and initialized).
Someone please correct me if I said anything wrong about the above steps.
Here is my suggested change to your code, I hope this helps.
import java.util.Scanner; public class CompoundInterest { public static void main(String[] args){ double rate; double amount; double year; System.out.println("This program, with user input, computes interest.\n" + "It allows for multiple computations.\n" + "User will input initial cost, interest rate and number of years."); Scanner input = new Scanner(System.in); System.out.println("What is the inital cost?"); amount = input.nextDouble(); System.out.println("What is the interest rate?"); rate = input.nextDouble(); rate = rate/100; System.out.println("How many years?"); year = input.nextDouble(); /* Calculate the interest over a number of 'years' and assign the value to 'finalAMount' */ double finalAmount = 0; // Perfectly legal to do this since finalAmount isn't used prior to this. for(int x = 1; x < year; x++){ finalAmount = amount * Math.pow(1.0 + rate, year); } System.out.println("For " + year + " years an initial " + amount + " cost compounded at a rate of " + rate + " will grow to " + finalAmount); } }



