Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Beginning Java and the fly likes Help Automatically Updating Value Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Introducing JavaFX 8 Programming this week in the JavaFX forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help Automatically Updating Value" Watch "Help Automatically Updating Value" New topic

Help Automatically Updating Value

Guy Burstein

Joined: Aug 18, 2011
Posts: 1
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.
Badal Chowdhary
Ranch Hand

Joined: Apr 26, 2011
Posts: 34


At line 23, while loop is hanging inside the class declaration. You might want to wrap it in a method.

At line 41, there is a semicolon followed by open parenthesis.


Campbell Ritchie

Joined: Oct 13, 2005
Posts: 44433
Welcome to the Ranch

"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

Joined: Oct 13, 2005
Posts: 44433
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

Joined: Oct 13, 2005
Posts: 44433
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().
I agree. Here's the link:
subject: Help Automatically Updating Value
It's not a secret anymore!