Q
Q
qdn144242015-12-13 15:51:34
Java
qdn14424, 2015-12-13 15:51:34

The variable does not save the value, I feel that I am stupid and it should not work, poke your nose why?

I learned the basics for a long time, now I'm trying to go back and write something, a lot has been forgotten and I don't understand why it doesn't work.
pseudo code:

.....
private class ParseTask extends AsyncTask<Context, Void, String>
    {  ......
        String resJson = "";
        .......

        boolean doSomethingAndExtractTo(String jsonAsString) // try to open a conn.
        {
           ....
          jsonAsString = mutableString.toString();
           .....
         }

       @Override
        protected String doInBackground(Context... params)
        { .....
           doSomethingAndExtractTo(resJson);
.....
         }
    }

I pass the existing resJson variable as a parameter, I expect that it will be assigned a value inside the method, it has a value inside the method.
but outside the method as if he did not write anything. I feel that this is not possible, probably through this.resJson = mutableString.toString(); you can, but why not?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
MiiNiPaa, 2015-12-13
@qdn14424

In Java, all objects are references passed as values.
jsonAsString = mutableString.toString();will make the local jsonAsString variable refer to the new string. Nothing will happen to the original string.

S
Sergey Popov, 2015-12-13
@be_a_dancer

Let the method return a value

String doSomethingAndExtractTo(String jsonAsString){
return mutableString.toString();
}
public static void main(String[] args){
resJson = "TextGoesHere";
resJson =  doSomethingAndExtractTo(resJson);
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question