sireesh Don wrote:
...
t3.setVal("sdfsa"); ---> if t3 value set why t1 , t2 object values setting according to t3.
in this program what i have to do even though t3 value set , it should not update the t1,t2 objects
i have to have t2 value in t3.
it is some thing like my main object should remain same .. i want to do some operations on temporary object which was get assigned from main object...
becoz of temporary object my main object should not change..
...
You see- t1,t2,t3 all point to the same instance. In
java its pass by value. So when you are passing around the reference variables through the methods- they would all refer to the same instance on the heap- the reason is simple- The value which is stored is the address of the instance and gets copied to multiple references. As Henry has mentioned- the link explains this pretty well.
Coming to your temporary object- You can use clone(). Read more about it from the API- Cloneable. But you make changes in your logic to actually avoid this kind of dependency.