title: How to set local variable inside callback function in Java / Android?
body: <p>How can I set the local variable <strong>publicIpAddress</strong> inside the onResponse() callback below? The line "publicIpAddress = response" has the error <em>"final local variable publicIpAddress cannot be assigned, since it is defined in an enclosing type"</em></p>
  
  <pre><code>public static String getPublicIpAddress(Context context)
      {
          String publicIpAddress = "";
  
          StringRequest jsonObjectRequest = new StringRequest(Request.Method.GET, 
                                                                      "http://icanhazip.com/", 
                                                                      new Response.Listener&lt;String&gt;() 
          {
              @Override
              public void onResponse(String response) {
                  publicIpAddress = response;
              }                       
          }, new Response.ErrorListener() {
              @Override
              public void onErrorResponse(VolleyError error) {
                  Log.e(TAG, "Error in getPublicIpAddress()");
              }
          });
  
          VolleySingleton.getInstance(context).addToRequestQueue(jsonObjectRequest);
  
          return publicIpAddress;
      }
  </code></pre>