java - Why doesn't pass-by-refernce work? -


this question has answer here:

i understand passed reference in java. why doesn't work in case? had thought should print out "hate" instead of "love".

class test {     static class str {         public string str;          public void set(string str) {             this.str = str;         }     }      public static void main(string[] args) {         str s = new str();          string str = "love";          s.set(str);         str = "hate";          system.out.println(s.str);     } } 

in main function, str stores reference string. when doing str = "hate", reference changes original object "love" has been stored in s.str , remains there.

see this more clarification.


Comments

Popular posts from this blog

c - How to retrieve a variable from the Apache configuration inside the module? -

c# - Constructor arguments cannot be passed for interface mocks -

python - malformed header from script index.py Bad header -