This could be a way if you want to update a certain value in multiple places (e.g. by using a wrapper object). Of course there are waay more fancier ways to accomplish something simular.
class TestClass {
public Wrapper<String> foo = new Wrapper("foo");
public void makeFooBar() {
Wrapper<String> bar = foo;
bar.set("bar");
System.out.println(foo); //should print "bar"
}
public static class Wrappper<T>
{
T value;
public Wrapper(T value)
{
this.value = value;
}
public T get() { return this.value; }
public void set(T newValue) { this.value = newValue; }
@override
public String toString() { return value.toString(); }
}
}
Basically any primitive type cannot be passed by reference. Althoug String is technically not a primitive type, java does treat it a bit more special. (Reading upon Internal Stringpool would give you a nice insight. In this page there is a nice explanation including some visual examples: http://www.journaldev.com/797/what-is-java-string-pool)