Anonymous functions

I want to do something like this

private ExtendedBoolean b = new ExtendedBoolean(){return m_pressed};
private void run()
    if(b){
        motor.set(1);
    }
}

but the only way i can do it now is

private ExtendedBoolean b = new ExtendedBoolean(){private boolean get() return m_pressed;};
private void run()
    if(b.get()){
        motor.set(1);
    }
}

which just looks ugly in my opinion. Is there a good way to do it?

This is a “good” way to do it.

However, you could make it look a lot “neater” if you changed the “get” method’s name to “pressed”. That way the if statement would read more like English would.

I’m generally of the opinion that little bits of ugliness like that are necessary evils in programming. While I’d love it if everything could be simply and elegantly expressed, reality tends to disregard my desires. However, the example you gave was such that the only non-“ugly” solution was what you suggested, which isn’t possible in Java. Depending on what you’re trying to do (as opposed to how you’re trying to make it look), there might actually be an elegant solution.