引用内部类中的非最终变量数据

发布于 2024-09-05 15:59:18 字数 968 浏览 5 评论 0原文

我正在用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

远山浅 2024-09-12 15:59:18

即使您设法克服了错误,该函数也不会执行您想要执行的操作。
您正在创建的回调将异步收到从服务器收到的响应通知,而您的方法 populateRSSData() 将立即返回。

您需要重新考虑您的设计,考虑到异步性。

编辑:请参阅习惯异步调用< /a>

编辑:
引用上述链接

要理解的重要问题是,当到服务器的实际往返仍在进行时,RPC 调用调用后面的代码将被执行。尽管 onSuccess() 方法内的代码是与调用内联定义的,但只有调用代码返回到 JavaScript 主循环并且服务器返回结果消息时,它才会执行。

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

The important issue to understand is that that the code that follows the RPC call invocation will be executed while the actual round trip to the server is still in progress. Although the code inside the onSuccess() method is defined inline with the call, it will not be executed until both the calling code returns back to the JavaScript main loop, and the result message from the server returns.

挥剑断情 2024-09-12 15:59:18

一个简单的方法 - 将 data 声明为字符串列表并将该列表设为最终列表。您始终可以将元素添加到最终列表(您只是不能为最终变量分配不同的列表)。

所以对于你的代码:

private String[] populateRSSData() {
  final List<String> data = new ArrayList<String>();
  try {
   new RequestBuilder(RequestBuilder.GET,
     "../database.php?action=populaterss").sendRequest(null,
     new RequestCallback() {

      @Override
      public void onResponseReceived(Request request,
        Response response) {
       data.addAll(Arrays.asList(response.getText().split("~"));
      }

      @Override
      public void onError(Request request, Throwable exception) {
       Window.alert(exception.getMessage());
      }
     });
  } catch (RequestException e) {
   Window.alert(e.getMessage());
  }

  return data.toArray(new String[data.size()]);
}

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:

private String[] populateRSSData() {
  final List<String> data = new ArrayList<String>();
  try {
   new RequestBuilder(RequestBuilder.GET,
     "../database.php?action=populaterss").sendRequest(null,
     new RequestCallback() {

      @Override
      public void onResponseReceived(Request request,
        Response response) {
       data.addAll(Arrays.asList(response.getText().split("~"));
      }

      @Override
      public void onError(Request request, Throwable exception) {
       Window.alert(exception.getMessage());
      }
     });
  } catch (RequestException e) {
   Window.alert(e.getMessage());
  }

  return data.toArray(new String[data.size()]);
}
走过海棠暮 2024-09-12 15:59:18

您需要为可变的 data 使用不同的数据结构 - 您可以修改 final 变量,但不能分配给它。也许 java.util.Vector 或类似的东西可以满足您的需求。

You need to use a different data structure for data which is mutable - you can modify a final variable, but not assign to it. Perhaps a java.util.Vector or similar would suit your needs here.

鸩远一方 2024-09-12 15:59:18

您应该使数据变量成为类成员(如果可能),或者用声明为 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文