Is there a way to overcome the fact that variables accessed in an Anonymous Inner Class have to be final?
For example I have a button that should report the size of the list anytime. In the while loop later on the list
should get modified and every cycle list
will be newly instantiated, which is not possible if list
is final
// has to be final
final ArrayList<String> list = new ArrayList<String>();
MyButton button = new MyButton() {
@Override
public int getValue() {
return list.size();
}
};
while (true) {
// modify/re-assign list
}
A way to do this is making list
a static variable, but thats not optimal. Is there another way to overcome this?