Java, “Variable name” cannot be resolved to a variable
up vote
15
down vote
favorite
I use Eclipse using Java, I get this error:
"Variable name" cannot be resolved to a variable.
With this Java program:
public class SalCal {
private int hoursWorked;
public SalCal(String name, int hours, double hoursRate) {
nameEmployee = name;
hoursWorked = hours;
ratePrHour = hoursRate;
}
public void setHoursWorked() {
hoursWorked = hours; //ERROR HERE, hours cannot be resolved to a type
}
public double calculateSalary() {
if (hoursWorked <= 40) {
totalSalary = ratePrHour * (double) hoursWorked;
}
if (hoursWorked > 40) {
salaryAfter40 = hoursWorked - 40;
totalSalary = (ratePrHour * 40)
+ (ratePrHour * 1.5 * salaryAfter40);
}
return totalSalary;
}
}
What causes this error message?
java variables name-resolution
add a comment |
up vote
15
down vote
favorite
I use Eclipse using Java, I get this error:
"Variable name" cannot be resolved to a variable.
With this Java program:
public class SalCal {
private int hoursWorked;
public SalCal(String name, int hours, double hoursRate) {
nameEmployee = name;
hoursWorked = hours;
ratePrHour = hoursRate;
}
public void setHoursWorked() {
hoursWorked = hours; //ERROR HERE, hours cannot be resolved to a type
}
public double calculateSalary() {
if (hoursWorked <= 40) {
totalSalary = ratePrHour * (double) hoursWorked;
}
if (hoursWorked > 40) {
salaryAfter40 = hoursWorked - 40;
totalSalary = (ratePrHour * 40)
+ (ratePrHour * 1.5 * salaryAfter40);
}
return totalSalary;
}
}
What causes this error message?
java variables name-resolution
add a comment |
up vote
15
down vote
favorite
up vote
15
down vote
favorite
I use Eclipse using Java, I get this error:
"Variable name" cannot be resolved to a variable.
With this Java program:
public class SalCal {
private int hoursWorked;
public SalCal(String name, int hours, double hoursRate) {
nameEmployee = name;
hoursWorked = hours;
ratePrHour = hoursRate;
}
public void setHoursWorked() {
hoursWorked = hours; //ERROR HERE, hours cannot be resolved to a type
}
public double calculateSalary() {
if (hoursWorked <= 40) {
totalSalary = ratePrHour * (double) hoursWorked;
}
if (hoursWorked > 40) {
salaryAfter40 = hoursWorked - 40;
totalSalary = (ratePrHour * 40)
+ (ratePrHour * 1.5 * salaryAfter40);
}
return totalSalary;
}
}
What causes this error message?
java variables name-resolution
I use Eclipse using Java, I get this error:
"Variable name" cannot be resolved to a variable.
With this Java program:
public class SalCal {
private int hoursWorked;
public SalCal(String name, int hours, double hoursRate) {
nameEmployee = name;
hoursWorked = hours;
ratePrHour = hoursRate;
}
public void setHoursWorked() {
hoursWorked = hours; //ERROR HERE, hours cannot be resolved to a type
}
public double calculateSalary() {
if (hoursWorked <= 40) {
totalSalary = ratePrHour * (double) hoursWorked;
}
if (hoursWorked > 40) {
salaryAfter40 = hoursWorked - 40;
totalSalary = (ratePrHour * 40)
+ (ratePrHour * 1.5 * salaryAfter40);
}
return totalSalary;
}
}
What causes this error message?
java variables name-resolution
java variables name-resolution
edited Sep 15 '13 at 3:47
Eric Leschinski
84.6k36316270
84.6k36316270
asked Sep 28 '11 at 19:50
user820913
3282718
3282718
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
up vote
10
down vote
accepted
If you look at the scope of the variable 'hoursWorked' you will see that it is a member of the class (declared as private int)
The two variables you are having trouble with are passed as parameters to the constructor.
The error message is because 'hours' is out of scope in the setter.
add a comment |
up vote
8
down vote
public void setHoursWorked(){
hoursWorked = hours;
}
You haven't defined hours
inside that method. hours is not passed in as a parameter, it's not declared as a variable, and it's not being used as a class member, so you get that error.
add a comment |
up vote
3
down vote
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.
add a comment |
protected by Community♦ May 22 at 6:51
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
10
down vote
accepted
If you look at the scope of the variable 'hoursWorked' you will see that it is a member of the class (declared as private int)
The two variables you are having trouble with are passed as parameters to the constructor.
The error message is because 'hours' is out of scope in the setter.
add a comment |
up vote
10
down vote
accepted
If you look at the scope of the variable 'hoursWorked' you will see that it is a member of the class (declared as private int)
The two variables you are having trouble with are passed as parameters to the constructor.
The error message is because 'hours' is out of scope in the setter.
add a comment |
up vote
10
down vote
accepted
up vote
10
down vote
accepted
If you look at the scope of the variable 'hoursWorked' you will see that it is a member of the class (declared as private int)
The two variables you are having trouble with are passed as parameters to the constructor.
The error message is because 'hours' is out of scope in the setter.
If you look at the scope of the variable 'hoursWorked' you will see that it is a member of the class (declared as private int)
The two variables you are having trouble with are passed as parameters to the constructor.
The error message is because 'hours' is out of scope in the setter.
edited Sep 15 '13 at 3:49
Eric Leschinski
84.6k36316270
84.6k36316270
answered Sep 28 '11 at 19:59
Hugh Jones
2,133926
2,133926
add a comment |
add a comment |
up vote
8
down vote
public void setHoursWorked(){
hoursWorked = hours;
}
You haven't defined hours
inside that method. hours is not passed in as a parameter, it's not declared as a variable, and it's not being used as a class member, so you get that error.
add a comment |
up vote
8
down vote
public void setHoursWorked(){
hoursWorked = hours;
}
You haven't defined hours
inside that method. hours is not passed in as a parameter, it's not declared as a variable, and it's not being used as a class member, so you get that error.
add a comment |
up vote
8
down vote
up vote
8
down vote
public void setHoursWorked(){
hoursWorked = hours;
}
You haven't defined hours
inside that method. hours is not passed in as a parameter, it's not declared as a variable, and it's not being used as a class member, so you get that error.
public void setHoursWorked(){
hoursWorked = hours;
}
You haven't defined hours
inside that method. hours is not passed in as a parameter, it's not declared as a variable, and it's not being used as a class member, so you get that error.
edited Sep 15 '13 at 3:49
Eric Leschinski
84.6k36316270
84.6k36316270
answered Sep 28 '11 at 19:55
Marc B
311k31317416
311k31317416
add a comment |
add a comment |
up vote
3
down vote
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.
add a comment |
up vote
3
down vote
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.
add a comment |
up vote
3
down vote
up vote
3
down vote
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.
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.
answered Sep 15 '13 at 3:59
Eric Leschinski
84.6k36316270
84.6k36316270
add a comment |
add a comment |
protected by Community♦ May 22 at 6:51
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?