Why does the state of an object declared as final seem to be reset even when it's altered inside an anonymous function?
<p>I use "token", instance of the ValueContainer class declared as final, in order to save data received from a JSON request. Inside the anonymous function, Response.Listener(), I set the value of token and it works. </p>

<pre><code>Log.d("myTag", "token value1:" + token.getValue());
</code></pre>

<p>This line displays a correct value (non null). However, when I check the value of token.getValue() again (this time outside the anonymous function)</p>

<pre><code>Log.d("myTag", "token value2:" + token.getValue());
</code></pre>

<p>I get token.getValue() = null. I don't understand why token's value seems to be reset to null. Am I missing something?</p>

<p>The full code:</p>

<pre><code>public class MainActivity extends AppCompatActivity {

class ValueContainer&lt;T&gt; {
    private T value;

    public ValueContainer() { }

    public ValueContainer(T value) {
        this.value = value;
    }

    public T getValue() {
        return value;
    }

    public void setValue(T value) {
        this.value = value;
    }
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    final ValueContainer&lt;String&gt; token = new ValueContainer&lt;String&gt;();
    String getTokenUrl = "http://www.someurl.com/?somekey=somevalue";

    JsonObjectRequest getTokenRequest = new JsonObjectRequest(
            Request.Method.GET, getTokenUrl, null, new Response.Listener&lt;JSONObject&gt;() {

        @Override
        public void onResponse(JSONObject response) {
            try {
                token.setValue(response.getString("token"));
                Log.d("myTag", "token value1:" + token.getValue());

                mainTextView.append("Response: JsonObjectRequest successful: get_token: " + token.getValue() + "\n");
            } catch (JSONException e) {
                mainTextView.append("Response: JSONException: getString():" + e);
            }
        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            mainTextView.append("Response: JsonObjectRequest failed: " + error + "\n");
        }
    });
    Log.d("myTag", "token value2:" + token.getValue());
}
</code></pre>