Unusual behaviour of reference pointer

Can somebody explain me, why the value of c.getI() is not changed from "100" to "sdkf"? Since equating two objects will actually make the two references point to the same memory location? This is some strange behaviour in java? I am new to java and want to know why this peculiar thing is happening?

publicabstractclass Interfac

{

String i="10";

}

class Aextends Interfac

{

A()

{

i ="100";

}

}

class Bextends Interfac

{

B()

{

i ="200";

}

}

class C

{

A a ;

C()

{

a =new A();

}

public Interfac getInterfac()

{

return a;

}

public String getI()

{

return a.i;

}

}

publicclass Main{

publicstaticvoid main(String[] args)

{

A a =new A();

B b =new B();

b.i="sdkf";

C c =new C();

Interfac i1 = c.getInterfac();

i1 = b;

System.out.println(i1.i +":" + c.getI());

//System.out.println("hashcodes: " + i1.hashCode() + " b: " + b.hashCode());

}

}

[2558 byte] By [ritu_jainna] at [2007-11-27 5:44:31]
# 1
Short answer: getI method returns the value of the i variable in the (local to to c) object a which is "100". It never gets changed to "sdkf". Only b's variable i gets changed.
floundera at 2007-7-12 15:25:11 > top of Java-index,Java Essentials,New To Java...
# 2

Interfac i1 = c.getInterfac();

i1 = b;

The first line causes i1 to point to the same Interfac object that the reference returned by c.getInterfac() points to.

The second line causes i1 to point to the same Interfac object that b points to. It has no effect on c's Interfac object.

When you do x = y for reference types, you're just causing x to point to the same object that y points to. It doesn't change the object that x previously pointed to, and it doesn't affect any other references that are pointing to that object.

jverda at 2007-7-12 15:25:11 > top of Java-index,Java Essentials,New To Java...
# 3
thanks. i got it!
ritu_jainna at 2007-7-12 15:25:11 > top of Java-index,Java Essentials,New To Java...