suggestBox.addKeyUpHandler( new KeyUpHandler() {
public void onKeyUp(KeyUpEvent event) {
if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
String boxText = suggestBox.getText();
if (!boxText.equals("")) {
suggestPanel.add(checkBoxFactory(boxText, candidateNames));
suggestBox.setText("");
}
}
}
});
I haven’t been able to grasp why java forces me to declare the ArrayList (candiateNames) as final. Why is that?
It’s an inner class, passed into
addKeyUpHandler— all variables referenced outside an inner class need to be declared as final to be used within the inner class. This is because the local class instance must maintain a separate copy of the variable, as it may out-live the function; so as not to have the confusion of two modifiable variables with the same name in the same scope, the variable is forced to be non-modifiable.Simply do
final {type} {new-varname} = {old-varname};before calling the method that uses the inner class, and then use{new-varname}inside that.