0

I am trying to create an alias for a object variable in Java so that I can change one and have it affect the other. For example, in this snippet, I expect "b" to be printed out. Instead I get "a".

String s = new String("a");
String t = s;
t = new String("b");
System.out.println(s);    // Prints "a".

I thought that non-primitive types in Java were able to be referenced and "tied" together like this.

  • 4
    Strings are *immutable*. You can achieve this by using a mutable object.And for Strings, you can use reflection do achieve this (not recommended though) – TheLostMind Oct 12 '17 at 10:49
  • @TheLostMind I'm essentially trying to do this to my own data structure in a larger project. How could I go about making sure my DS is mutable? – Tristan Batchler Oct 12 '17 at 10:50
  • 2
    Besides the immutability of String, by executing `t = new String("b");` you assign a completly new instance to t. This would not overwrite the value, not even if you are using mutable objects. – Korashen Oct 12 '17 at 10:51
  • And for the issue itself, go look at a `facade`, `proxy` or `Delegate` design patterns. Or maybe an `Observable` is just what you need? – Korashen Oct 12 '17 at 10:52
  • It's no suprise that this prints `a`, because you never re-assign the `s` variable. If you would do `s = new String("b");`, it would indeed print `b`. – Mick Mnemonic Oct 12 '17 at 10:53
  • @Korashen Thank you! This was my problem. I was assigning a completely new instance to what I wanted to be the alias. Cheers. – Tristan Batchler Oct 12 '17 at 10:53
  • be precise with words. 'refference' in Java sense have some of meaning 'alias', but isn't in general – Jacek Cz Oct 12 '17 at 10:56
  • The question this is marked as a duplicate of is a really poor choice. This has nothing to do with String immutability, but with the concepts of values and references. OP believes his variables will act as pointers, but in Java there's no real pointer type. Better duplicate: https://stackoverflow.com/questions/1750106/how-can-i-use-pointers-in-java – G_H Oct 13 '17 at 05:12

3 Answers3

1

No, it never works like that. Variables, fields etc. cannot be "bound" in Java.

Your variables can contain "a link to a value" in a variable, but you cannot have a "link to another variable".

This means that in your case s and t initially hold the link to the same value, but they don't know about each other. When you assign a new string to t, the value of the old string does not change (and s still references the old value). It's just the reference in `t

fdreger
  • 12,264
  • 1
  • 36
  • 42
0

String is not good example, because this is immutable. You have to look at using object instances and object reference. One object instance could have multiple object references.

class Decorator {
    private String str;

    public Decorator(String str) {
        this.str = str;
    }

    public void setStr(String str) {
        this.str = str;
    }

    public String getStr() {
        return str;
    }
}


Decorator s = new Decorator("a"); // s - is a reference to Decorator
System.out.println(s.getStr());  // prints "a"
Decorator t = s; // t - is an another reference to existed Decorator (two references for one instance)
t.setStr("b");
// references s and t both point to one Decorator instance
System.out.println(s.getStr());  // prints "b"
System.out.println(t.getStr());  // prints "b"
Oleg Cherednik
  • 17,377
  • 4
  • 21
  • 35
0

You might be looking for something like this:

StringWrapper sw = new StringWrapper(new String("a"));
StringWrapper tw = sw;
tw.setS(new String("b"));
System.out.println(sw.getS()); // Prints b

class StringWrapper {
    private String s;
    public StringWrapper(String s) {
        this.s = s;
    }
    private String getS() {
        return s;
    }
    private void setS(String s) {
        this.s = s;
    }
}

(You can replace new StringWrapper(new String("a")) with new StringWrapper("a"). I added new String so it looks more like your example. Same goes for new String("b").)

Stefan
  • 2,395
  • 4
  • 15
  • 32