In the above code, I have two instance variables, one a primitive type and another a String variable. After calling test(), the integer variable remains the same as expected. But the String reference also remains the same (prints "hello"). Shouldn't the modification made in the method test() be reflected, i.e., it should have printed "again_hello" instead of "hello". This is what shallow copying is, right?
CloneTest ct1 is declared and initialized within method: test(Object obj) so after leaving this method ct1 (and its string str) do not exist anymore. Isn't this thinking correct?
Vijay R. I am sorry to inform you that a last initial is not the same as a last name. The JavaRanch Name Policy requires a first name, a space, and a last name. You can change it here.
JavaBeginnersFaq "Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
CloneTest ct = new CloneTest(); ct.test(ct.clone()); out.println(ct.count); out.println(ct.str);
In ct.test(ct.clone()) I make a copy (ct.clone()), I then change the copy (test(ct.clone)) an then I look at the original's members, not those of the copy!
If I change the code to this:
I get this print-out: ct:10 ct:hello ctx:20 ctx:again_hello
which is what was wanted, yes?
You will always be treated with dignity. Now, strip naked, get on the probulator and hold this tiny ad:
Gift giving made easy with the permaculture playing cards