BaconMan97
BaconMan97

Reputation: 67

If statement isnt working even when i set a boolean to automatically be false

grass.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
    pm = false;
    if(pm=false){
    drawGrassBlock(x1, y, Color.green, size, size2);
    }
    }
});

This is my code and for whatever reason the if statement does nothing....i set the boolean to be false and told it to print a string when inside the if statement but the console wont return anything which means its not properly "getting inside". I told it to print the value of the boolean and its printing false so please help!

Upvotes: 1

Views: 228

Answers (2)

hmjd
hmjd

Reputation: 121961

That is an assignment within the if, not an equality comparision. From section 15.26 Assignment Operators of the Java Language Specification:

At run time, the result of the assignment expression is the value of the variable after the assignment has occurred.

meaning the result of pm = false is always false, so the if branch is never entered.

Change to:

if (false == pm) // or if (!pm)

Upvotes: 2

juergen d
juergen d

Reputation: 204746

Do

if(pm == false)

instead of

if(pm = false)

Using single = will set pm to false
Using double == will compare pm with false

Upvotes: 2

Related Questions