[java] 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?

This question is related to java variables scope

The answer is


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.


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.


Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to variables

When to create variables (memory management) How to print a Groovy variable in Jenkins? What does ${} (dollar sign and curly braces) mean in a string in Javascript? How to access global variables How to initialize a variable of date type in java? How to define a variable in a Dockerfile? Why does foo = filter(...) return a <filter object>, not a list? How can I pass variable to ansible playbook in the command line? How do I use this JavaScript variable in HTML? Static vs class functions/variables in Swift classes?

Examples related to scope

Angular 2 - Using 'this' inside setTimeout Why Is `Export Default Const` invalid? How do I access previous promise results in a .then() chain? Problems with local variable scope. How to solve it? Why is it OK to return a 'vector' from a function? Uncaught TypeError: Cannot read property 'length' of undefined Setting dynamic scope variables in AngularJs - scope.<some_string> How to remove elements/nodes from angular.js array Limiting number of displayed results when using ngRepeat A variable modified inside a while loop is not remembered