I have class SpeexRunner as follows, The constructor of takes two arguments a boolean variable and a LinkedList<short[]>. As follows :-
public class SpeexRunner implements Runnable {
public boolean stopThread;
LinkedList<short[]> dataList;
public SpeexRunner(boolean val_stopThread, LinkedList<short[]> dataRef){
this.stopThread = val_stopThread;
dataList = dataRef;
}
@Override
public void run() {
//add objects in dataList;
// change / remove dataList Objects
}
}
My Question is:- If I change the dataList inside the run(), will the changes be reflected to the original list which is declared somewhere else ?
Yes. Your constructor receives a reference to the list, not a copy of it. If you want to copy it, you’ll have to use the
LinkedListcopy constructor. Then you’ll have your own copy of the list. But note that the entries on the two lists are still shared, because the entries are arrays (short[]), and arrays are stored by reference.This is perhaps best demonstrated by example:
When
DirectUserused the list, in our calling code we saw changes both to the list (in that it got longer) and to its contents (the first entry’s first slot changed from0to1).When
CopyListUserused it, it made a copy of the list, so we didn’t see any change to our list in our calling code (it didn’t get longer). But we did see the change to the first entry (because both lists shared the same array object) — the first slot changed from0to1again.When
DeepCopyUserused it, it made a copy of the list and a copy of each entry, so things were completely and totally disconnected. No changes to the list or to its items were seen by our calling code.