This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
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 The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help Automatically Updating Value" Watch "Help Automatically Updating Value" New topic
Author

Help Automatically Updating Value

Guy Burstein
Greenhorn

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

Guy,

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.

Thanks,
Badal


Blog: http://badalchowdhary.wordpress.com/
Twitter: http://twitter.com/badalrocks
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38449
    
  23
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
Sheriff

Joined: Oct 13, 2005
Posts: 38449
    
  23
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
Sheriff

Joined: Oct 13, 2005
Posts: 38449
    
  23
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 sorta covered in the JavaRanch Style Guide.
 
subject: Help Automatically Updating Value