如果我有这个匿名方法,我应该将 x 变量声明为 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();
}
});
}
但是,如果 loader 是一个类字段,则没有必要将其声明为 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();
}
});
//Could I modify loader's reference here, before the method executes?
//loader = null;
}
}
有谁知道为什么他们保证局部变量在被访问时不会改变但不为类字段做这件事?