Hi,
I need to pass a String object to a class method, and have the String set in that method. However, while the string "appears" to be properly set in the method, upon return to the calling code it assumes its old value. For example:
The output is then:
value is first value
1 value is first value
2 value is bar
last is first value
How can I have the class B method assign the new string "permanently" to s? I know strings are immutable, but it was (i thought) the case that they are referene objects - and hence can be assigned new references (even though what they point to can't be changed). Hence, the method call should create a new string object, and point the parameter-string to it...
I'm a relative newby to java, any/all help appreciated!
Thanks,
dora
I need to pass a String object to a class method, and have the String set in that method. However, while the string "appears" to be properly set in the method, upon return to the calling code it assumes its old value. For example:
Code:
public class A
{
...
public void foo ()
{
String s = "first value";
System.out.println("value is: " + s);
B b = new B();
b.Set(s);
System.out.println("last is: " + s);
};
....
}
public class B
{
....
public boolean Set ( String s )
{
System.out.println("1 value is: " + s);
s = "bar";
System.out.println("2 value is: " + s);
};
....
};
The output is then:
value is first value
1 value is first value
2 value is bar
last is first value
How can I have the class B method assign the new string "permanently" to s? I know strings are immutable, but it was (i thought) the case that they are referene objects - and hence can be assigned new references (even though what they point to can't be changed). Hence, the method call should create a new string object, and point the parameter-string to it...
I'm a relative newby to java, any/all help appreciated!
Thanks,
dora