I have played with the following code, partially derived from Mughal p.49, and I still can't explain to myself the rationale for the result.
public class Tester {
int i = 1;
public static void main (
String[] args) {
String a,b,c;
c = new String("mouse");
a = new String("cat");
b = a;
a = new String("dog");
c = b;
//c = "cat", but I am expecting "dog" since c references b and b references a and a
//reassigned the string it refered to to be "dog".
System.out.println ("c = " + c);
// --------------------------------------------------------
Tester tc = new Tester();
Tester ta = new Tester();
Tester tb = new Tester();
tb = ta; //assign b to a, just like above
ta.i = 2; //change a, just like above
tc = tb; //assign c to b, just like above
//Expecting tc.i to equal 2, since it points to tb which points to ta which
//reassigned i to equal 2. This worked as I expected.
System.out.println("tc.i = " + tc.i);
}
}
Having played with this for over an hour and examining Mughal's explanation on p. 625, in addition to RHI and The
Java Tutorial (Sun), I am absolutely baffled. I love this language!
[This message has been edited by Michael Morett (edited November 04, 2001).]
[This message has been edited by Michael Morett (edited November 04, 2001).]