PDA

View Full Version : Java string question



g3k0
March 14th, 2009, 11:54 PM
Hey,
I am setting a string in java equal to another string. However, it seems to be only changing the reference.

I have two objects of the same type. Each have a string called name.
I have a replace function that does this


public void replace(String newName){
this.name = newName;
}

If I call it, both objects name are changed to that value. How do I make it copy a string instead of the reference? I only want the one to change.

kjohansen
March 15th, 2009, 12:02 AM
in java strings are immutable, thus you always change the reference not the value. but your function should set the reference of only one of the instances not both, so I dont think the problem is in the function you showed...

myrtle1908
March 15th, 2009, 12:07 AM
this refers to the current class instance, so when you write


this.name = newName;

you are setting the class level variable named 'name' to the variable 'newName' thus any subsequent accesses to this.name will reflect the change.

You probably want to localise the name variable to the replace method or maintain two class level variables.

g3k0
March 15th, 2009, 12:10 AM
sorry. Perhaps I was unclear with the post. I just want the two objects to have different values for the strings that they have declared within them.


Class Example{
Private String name;
public void replace(String newName){
this.name=newName;
}
}


When I pass a string to both objects for some reason they always have the same value. I can't figure out why...

so if I pass in "fred" to one object it is fred
then if I pass "steve" to the other object they are both steve!

eye208
March 15th, 2009, 01:15 AM
When I pass a string to both objects for some reason they always have the same value. I can't figure out why...

so if I pass in "fred" to one object it is fred
then if I pass "steve" to the other object they are both steve!
I guess you did something like this:

Example a = new Example();
Example b = a;
a.replace("fred");
b.replace("steve");
a and b are not independent copies but refer to the same instance of the Example class. To make independent copies, you have to implement the Cloneable interface, add a public clone() method, and use that to make the copy like this:

Example b = a.clone();

g3k0
March 15th, 2009, 02:05 AM
Thats not exactly what I did, but you showed me what the problem was (the object had same ref). I was doing something.push(new object(new object1(Object2.atype))) and object2 had the string in it I was trying to change. I was thinking it was the string I was passing not the object itself...

Thanks for the help