Java changes reference but not the object itself?
问题 Please see the following code: public static void main(String[] args) { Test t1 = new Test(); t1.i = 1; Test t0 = t1; Test t2 = new Test(); t2.i = 2; t0 = t2; System.out.println(t1.i); //prints 1, I thought it would print 2 } Is there any way to change t1 object after I got it from somewhere else by simple assignment without accessing t1 directly? (i.e. t1 = t0) 回答1: Here is an illustration of your program and what it does. Each state of the system is shown separated by a dashed line. Note