Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If I understand your question correctly, you're asking how you can write a variable from inside an anonymous class.</p> <p>Anonymous classes can only access final variables, and can't directly "write" them.</p> <p>A straightforward solution that is "good enough" is to create sort of a ValueBox class with a single value field and a getter and setter. You can then instantiate a new one in the function as a final variable, and have your anonymous class access it. The anonymous class will use its getter and setter to write/read.</p> <p>The fact that the variable is final just means that you can't aim the reference anywhere else, but you can still change the contents of the referred object from either function.</p> <p>The bigger problem you are going to have is in waiting until the callback has been called. This sort of wait-sleep might be good enough, but you may want to consider timeouts, threads, etc, depending on what you're trying to achieve.</p> <p>In addition, this all assumes that you are never going to call this twice on the connection. Otherwise, you need to provide more info to us on your synchronization model.</p> <p>Here's some sample code:</p> <pre><code>public int getUserid(String username) { final ValueBox&lt;Integer&gt; userid = new ValueBox&lt;Integer&gt;(); connection.call("getUserid", new Responder() { public void onResult(final int result) { System.out.println("userid: " + result); userId.setValue(result); //how to assign received value to userid and return it? } }, username); //wait for response while (userid.isEmpty()) { try{ Thread.sleep(100); } catch (Exception e) {} } return userid.getValue(); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload