Java Cannot Be Resolved To A Variable, Eclipse Error

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) { salaryAfter40 = hoursWorked – 40; totalSalary = (ratePrHour * 40) + (ratePrHour * 1.5 * salaryAfter40); } return totalSalary; }}What causes this error message?

java variables scope
Improve this question
edited Sep 6 “19 at 3:10


Bhargav Rao♦
40.6k2626 gold badges111111 silver badges128128 bronze badges
asked Sep 28 “11 at 19:50


61333 gold badges1111 silver badges2121 bronze badges
Add a comment |

3 Answers 3

Active Oldest Votes
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.

Đang xem: Java cannot be resolved to a variable

The error message is because “hours” is out of scope in the setter.

Improve this answer
edited Sep 15 “13 at 3:49


Eric Leschinski
123k8181 gold badges380380 silver badges321321 bronze badges
answered Sep 28 “11 at 19:59


Hugh JonesHugh Jones
2,5641313 silver badges2828 bronze badges
Add a comment |
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.

Read more: How To Power Carpenter Minecraft, : Feedthebeast

Improve this answer
edited Sep 15 “13 at 3:49


Eric Leschinski
123k8181 gold badges380380 silver badges321321 bronze badges
answered Sep 28 “11 at 19:55
Marc BMarc B
340k3636 gold badges381381 silver badges468468 bronze badges
Add a comment |
I”ve noticed bizarre behavior with Eclipse version 4.2.1 delivering me this error:

String cannot be resolved to a variableWith 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.

Read more: wow best hunter race alliance

Improve this answer
answered Sep 15 “13 at 3:59
Eric LeschinskiEric Leschinski
123k8181 gold badges380380 silver badges321321 bronze badges
Add a comment |
Highly active question. Earn 10 reputation in order to answer this question. The reputation requirement helps protect this question from spam and non-answer activity.

Not the answer you're looking for? Browse other questions tagged java variables scope or ask your own question.

The Overflow Blog
Featured on Meta
Visit chat
beginner do while loop error
textArea can not be resolved java
“variable” cannot be a resolved to a variable
How to set Date? java.util.Date validFrom – valdiFrom cannot be resolved to a variable
How to access Hashmap from class to JavaFX class
“input cannot be resolved” using methods
use method of one class in another class of different package
How can I call android system service like wifi_service in my uiautomator project?
Taking in Webdriver element name as a parameter
Practice Java problems
See more linked questions
What are the differences between a HashMap and a Hashtable in Java?
Is Java “pass-by-reference” or “pass-by-value”?
Avoiding NullPointerException in Java
How do I read / convert an InputStream into a String in Java?
When to use LinkedList over ArrayList in Java?
How do I generate random integers within a specific range in Java?
How can I determine if a variable is 'undefined' or 'null'?
How can I create a memory leak in Java?
Can't execute jar- file: “no main manifest attribute”
Can't start Eclipse – Java was started but returned exit code=13
Hot Network Questions more hot questions
Question feed
Subscribe to RSS
Question feed To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

Stack Overflow
Stack Exchange Network
site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev2021.4.23.39140

Stack Overflow works best with JavaScript enabled


Your privacy

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Leave a Comment