LoreleiRS
LoreleiRS

Reputation: 103

Vairables used but eclipse says they're not?

I'm doing some input handling in Java and I'm running into some trouble. I've got two variables (xMove and zMove) and I've used them in the code a number of times yet eclipse refuses to acknowledge their use. I have a feeling that is what's causing me problems so please take a look so I can see what I'm doing wrong

    public void tick(boolean forward, boolean back, boolean left, boolean right, boolean turnLeft, boolean turnRight) {
        double xMove = 0.0;
        double zMove = 0.0;

        if (forward) {
            zMove++;
        }
        if (back) {
            zMove--;
        }
        if (left) {
            xMove++;
        }
        if (right) {
            xMove--;
        }
        if (turnLeft) {
            zMove++;
        }
        if (turnRight) {
            zMove++;
        }

    }

Upvotes: 0

Views: 65

Answers (3)

yshavit
yshavit

Reputation: 43401

Think in terms of observable behavior: that is, behavior you can actually see or measure from "outside" the program somehow. For instance, System.out.println("hello") has the observable behavior of printing "hello" to stdout. Of course, some actions are less direct. Given this snippet:

if (i < 0) {
    i++;
}
System.out.println(i);

... the action i++ doesn't have a directly observable outcome. However, if you can put your program into a state where i < 0, you can then observe the outcome that the printed number is one greater than the one the program would have had, if not for the if block.

With that in mind, what's the observable outcome of this code:

public void tick1(boolean up) {
    int i = 1;
    if (up) {
        i++;
    }
}

vs, what's the observable outcome of this code:

public void tick2(boolean up) {
    int i = 1;
}

As you can maybe see, their observable outcomes are the same: nothing changes. Even if i is at its maximum value and up is true, tick1 will not throw an exception -- it will just overflow i, and then do nothing with the result.

In fact, if you had:

public void tick3(boolean up) {
    // don't even declare i
}

... this third variant has exactly the same observable outcome: nothing changes, still.

Your IDE is trying to tell you that there is probably one of two situations going on in your code:

  • you meant to use the variable (to some observable end), but didn't; in this case, you should use it
  • you didn't meant to use the variable; in which case you might want to simplify the code by just removing it

The code you posted is essentially in the same situation.

Now, if you simplified the tick1 and tick2 to get tick3, your IDE might then tell you that up is unused. Consider this last variant:

public void tick4() {
    // don't take any arguments or do anything
}

Even though this last variant has different compile-time behavior (it takes one less arg), actually has the same runtime behavior as the first three.

Upvotes: 1

James Madison
James Madison

Reputation: 943

Because you're not using them! Yes, you increment and decrement them, so, yes, you think of that as changing in one sense. But then what do you do with them? You don't assign them to any other variable. You don't return them from the method. Where do they actually go now that you've incremented/decremented them? No where. You are, in fact, not using them! :-)

Try assigning them to another variable. Or returning them. Or assigning to a class-level variable. Do something else with them, and this should clear up.

Upvotes: 6

Surangie
Surangie

Reputation: 59

Try storing the variables outside the method. Then, as XWaveX says They Will not be initialized to zero everytime you call the method.

Upvotes: 0

Related Questions