Help Automatically Updating Value

I've been trying to figure out how to automatically update a Boolean variable. Whenever I try to compile it however I get the error "illegal start of type". Can someone tell me where I went wrong here? Thank you.

"Illegal start ..." usually mean you are trying to use keywords like public or static inside a method. That happens if you have not balanced your {s and }s correctly.

Campbell Ritchie

Marshal

Posts: 57446

175

posted 6 years ago

By the way, never write == true or == false. Not only are they poor style, but they can cause nasty errors if you write = by mistake. As I think you have already done.

It's not if (b == true) ...; it's if (b) ... Not if (b == false) ... but if (!b) ...

Beware of using if else to assign to booleans. You can reduce your loop to isMoving = speed > 0; Of course, you are never calling that loop from any of your methods, and the isMoving field is redundant. You always have a speed field, so you can actually dispense with the isMoving field altogether, and replace it with speed > 0 throughout.

Alternatively, try this:Now you can replace isMoving with isMoving() throughout. That would probably be better. Get rid of the isMoving field.

Campbell Ritchie

Marshal

Posts: 57446

175

posted 6 years ago

Better than using printState() would be to override the toString() method. Now you can call it like thisBut you will only get a useful printout if you override toString().

It is difficult to free fools from the chains they revere - Voltaire. tiny ad: