I have this very simple code. I am trying to figure out how I can simultaneously alter the object reference in the array with the object reference that was first placed into the array. I want to know how to bind the two references/values.
In JavaScript, which is very similar to Java in terms of passing by value, the best we probably can do is to use Object.observe() like so: http://www.html5rocks.com/en/tutorials/es7/observe/
but with Java, I wonder if there is a good way to do this without writing your own library?
public class ArrayRefVal {
public static void main(String[] args) {
ArrayList<Object> al = new ArrayList<Object>();
Object s = new Object();
al.add(s);
s = null;
System.out.println(al.get(0));
}
}
likewise:
public class ArrayRefVal {
public static void main(String[] args) {
ArrayList<Object> al = new ArrayList<Object>();
Object s = new Object();
al.add(s);
al.set(0, null);
System.out.println(s);
}
}
turns out JavaScript has the same behavior as Java on this one. How can I somehow bind the element in the array with the object s, such that when I change the element in the array it changes the original object?
Did I mention this already? I want to know how to bind the two references/values.