If I had this anonymous method, I have to declare the x variable as final.
private void testMethod (ListField<BeanModel> listField){ final ListLoader<BeanModel> loader = new PagedListLoader<BeanModel>(); listField.addListener(Events.Attach, new Listener<ListViewEvent<BeanModel>>() { @Override public void handleEvent(ListViewEvent<BeanModel> be) { loader.load(); } }); }
However, if loader was a class field, there is no need to declare it as final:
public class testClass{ private ListLoader<BeanModel> loader = new PagedListLoader<BeanModel>(); private void testMethod (ListField<BeanModel> listField){ listField.addListener(Events.Attach, new Listener<ListViewEvent<BeanModel>>() { @Override public void handleEvent(ListViewEvent<BeanModel> be) { loader.load(); } });
Does anyone know the reason why they guarantee that local variables will not change when they are accessed, but do not do this for class fields?
source share