引用内部类中的非最终变量数据
我正在用 GWT 编写一个程序。这是我遇到问题的代码片段
private String[] populateRSSData() {
1==>> String[] data = null;
try {
new RequestBuilder(RequestBuilder.GET,
"../database.php?action=populaterss").sendRequest(null,
new RequestCallback() {
@Override
public void onResponseReceived(Request request,
Response response) {
2==>> data=response.getText().split("~");
}
@Override
public void onError(Request request, Throwable exception) {
Window.alert(exception.getMessage());
}
});
} catch (RequestException e) {
Window.alert(e.getMessage());
}
return data;
}
现在问题出现了,我收到一个错误,变量 1==>>
data
应该声明 final< /代码>。但是,如果我将其声明为
final
,那么我无法将数据存储在 2==>>
中,
我得到的错误
Cannotrefer to a non-final variable在不同方法 RSS_Manager.java 中定义的内部类内的数据
请建议
Im doing a program in GWT. Here is the snippet where Im having problem
private String[] populateRSSData() {
1==>> String[] data = null;
try {
new RequestBuilder(RequestBuilder.GET,
"../database.php?action=populaterss").sendRequest(null,
new RequestCallback() {
@Override
public void onResponseReceived(Request request,
Response response) {
2==>> data=response.getText().split("~");
}
@Override
public void onError(Request request, Throwable exception) {
Window.alert(exception.getMessage());
}
});
} catch (RequestException e) {
Window.alert(e.getMessage());
}
return data;
}
Now the problem arises that I get an error that the variable 1==>>
data
should be declared final
. But if I declare it as final
then i cannot store the datas in 2==>>
The error i get
Cannot refer to a non-final variable data inside an inner class defined in a different method RSS_Manager.java
Please suggest
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
即使您设法克服了错误,该函数也不会执行您想要执行的操作。
您正在创建的回调将异步收到从服务器收到的响应通知,而您的方法 populateRSSData() 将立即返回。
您需要重新考虑您的设计,考虑到异步性。
编辑:请参阅习惯异步调用< /a>
编辑:
引用上述链接
Even if you manage to get over the error, the function will not do what you intend it to do.
The callback you are creating will be notified asynchronously of the response received from server while your method populateRSSData() will return immediately.
You need to rethink your design taking the asynchrony into account.
EDIT: see Getting Used to Asynchronous Calls
EDIT:
Quote from the above link
一个简单的方法 - 将
data
声明为字符串列表并将该列表设为最终列表。您始终可以将元素添加到最终列表(您只是不能为最终变量分配不同的列表)。所以对于你的代码:
An easy way out - declare
data
as a list of Strings and make the list final. You can always add elements to final list (you just can`t assign a different list to the final variable).So for your code:
您需要为可变的
data
使用不同的数据结构 - 您可以修改final
变量,但不能分配给它。也许java.util.Vector
或类似的东西可以满足您的需求。You need to use a different data structure for
data
which is mutable - you can modify afinal
variable, but not assign to it. Perhaps ajava.util.Vector
or similar would suit your needs here.您应该使数据变量成为类成员(如果可能),或者用声明为 Final 的某个对象包装它。
局部内部类中使用的变量必须声明为final。另请参阅:方法局部内部类访问方法的局部变量。
--编辑--
塔希尔·阿赫塔尔指出了一些非常重要的事情。除了我提到的一般要点之外,您的设计中还有一个缺陷 - 回调用于异步响应。响应可能在方法终止后很久才到达,因此结果尚未分配。
You should either make the data variable a class member (if possible), or wrap it with some object declared as final.
Variables used inside local inner classes must be declared as final. See also: method local innerclasses accessing the local variables of the method.
--EDIT--
Tahir Akhtar pointed out something very important. Besides the general points I mentioned, there is a flaw in your design - the callback is used for asynchronous response. The response may arrive long after the method terminates, so the result will not be assigned yet.