variable assignment - Field Overwrite does not work within a method body - Java -
here's question goes right basics (i think) although had me stumped in recent coding project undertook few friends.
here's code variation one:
public class test { private string test; public test(){ test = "tester"; changestring(test); } public void changestring(string t){ t = "blue apples"; } public string gettest(){ return test; } public static void main(string[] args){ test t = new test(); system.out.println(t.gettest()); } }
why program print out "tester" instead of "blue apples"? shouldn't method changestring(string) turn field 'test' "blue apples"?
thanks responses in advance!
java pass value , not pass reference. therefore, changes made passed t
string, won't reflected in test
string.
public void changestring(string t){ t = "blue apples"; test = t; // include line assign value of `t` `test`. }
Comments
Post a Comment