Search code examples
javagwtsmartgwt

Error: local variable event is accessed from within inner class; needs to be declared final


I have this code to ask the user to confirm when deleting a row.

 @Override
    public void onRemoveRecordClick(RemoveRecordClickEvent event)
    {
        SC.confirm("Are you sure to delete ?", new BooleanCallback() {
            public void execute(Boolean value) {
                if (value != null && value) {
                    event.cancel();
                    removeData(getDataAsRecordList().get(event.getRowNum()));
                } else {

                }
            }
        });
    }

But I got the error local variable event is accessed from within inner class; needs to be declared final

How can I fix this?


Solution

  • Make this change:

    public void onRemoveRecordClick(final RemoveRecordClickEvent event)
    

    The method execute() needs a guarantee that the variable event is not going to disappear (be reassigned) by the time it is needed. You provide this guarantee by declaring it as final. Now the compiler knows where to find this variable once this method needs it.