Why not simply return, i.e.,
public void updateEvent() {
//object's update event
remove(this);
return;
//obviously unreachable
System.out.println("Should never reach here!"); }
Why not simply return, i.e.,
public void updateEvent() {
//object's update event
remove(this);
return;
//obviously unreachable
System.out.println("Should never reach here!"); }
Why don't you just:
@jball's answer is great-- +1.
Another method I've used that works well and might be slightly cleaner is to have your updateEvent() method return a boolean. Whenever a True is returned from updateEvent, you remove() the object.
This allows your event loop to have more control over how the loop itself is executed and removes a little bit of unnecessary binding between your classes.
I agree that the exception approach is the best way to implement the remove method according to your specification.
However, maybe you should reconsider the specification. I would leave the decision of when updateEvent terminates to the implementor. Termination with the remove() call is confusing, and requires usage of exceptions for flow control. I believe that the call to remove() should only change the flag state. And I see no real problem with looping over all objects, checking the removed flag for each of them.