简体   繁体   中英

Why does the state of an object declared as final seem to be reset even when it's altered inside an anonymous function?

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.

Log.d("myTag", "token value1:" + token.getValue());

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

Log.d("myTag", "token value2:" + token.getValue());

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

The full code:

public class MainActivity extends AppCompatActivity {

class ValueContainer<T> {
    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<String> token = new ValueContainer<String>();
    String getTokenUrl = "http://www.someurl.com/?somekey=somevalue";

    JsonObjectRequest getTokenRequest = new JsonObjectRequest(
            Request.Method.GET, getTokenUrl, null, new Response.Listener<JSONObject>() {

        @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());
}

You can change the "real content" of a "final" Object, but you can't change it's address, as the example said blew.

 String stringTest = "test String";

 StringBuffer normalTest = new StringBuffer(stringTest);
 normalTest = normalTest.append(" end");
 normalTest.delete(0, 5);
 System.out.println(normalTest);

 final StringBuffer finalTest = new StringBuffer(stringTest);
// finalTest = finalTest.append(" end"); // can't work;
 finalTest.delete(0, 5);
 System.out.println(finalTest);

you can test by your code as

//token =  new Token("...")// can't work
token.changeTheToken("...")// work ok

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM