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
Share
Improve this question
Follow
edited Sep 6 “19 at 3:10

Bhargav Rao♦
40.6k2626 gold badges111111 silver badges128128 bronze badges
asked Sep 28 “11 at 19:50
user820913user820913
61333 gold badges1111 silver badges2121 bronze badges
0
Add a comment |
3 Answers 3
Active Oldest Votes
15
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.
Share
Improve this answer
Follow
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
0
Add a comment |
10
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
Share
Improve this answer
Follow
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 |
3
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
Share
Improve this answer
Follow
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
Linked
0
beginner do while loop error
0
textArea can not be resolved java
0
“variable” cannot be a resolved to a variable
0
How to set Date? java.util.Date validFrom – valdiFrom cannot be resolved to a variable
0
How to access Hashmap from class to JavaFX class
0
“input cannot be resolved” using methods
-3
use method of one class in another class of different package
-1
How can I call android system service like wifi_service in my uiautomator project?
0
Taking in Webdriver element name as a parameter
0
Practice Java problems
See more linked questions
Related
3936
What are the differences between a HashMap and a Hashtable in Java?
7014
Is Java “pass-by-reference” or “pass-by-value”?
4175
Avoiding NullPointerException in Java
4307
How do I read / convert an InputStream into a String in Java?
3282
When to use LinkedList over ArrayList in Java?
3708
How do I generate random integers within a specific range in Java?
2302
How can I determine if a variable is 'undefined' or 'null'?
3410
How can I create a memory leak in Java?
1109
Can't execute jar- file: “no main manifest attribute”
989
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.
lang-java
Stack Overflow
Products
Company
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.