Java, “Variable name” cannot be resolved to a variable


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?

Best Solution

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.