I am passing a vector into the set method of another class. I have a private variable in that class and I simply set that variable equal to the passed in vector. Bottom line: I had to clone the vector. The vector was being passed in by reference and so was effecting the Original vector. I thought stuff was passed by value in Java. What am I missing here?? Thanks.
A Vector is a reference to an Array of Objects. When you pass a Vector to a method a copy of the original reference gets passed. Both the original reference and the copy still reference the same Object Array. If you make changes to the Object array, the change occurs for both (it's the same Array). If you would change the Vector reference to a new Object Array in the method, then that change would not be reflected to the original. If you want to pass a new array then you have to pass a cloned copy of the original.
Parameters in Java are passed by value. When you pass in an object reference, you pass the value of that reference. If you change the value of that reference (i.e. make it point to something else) the original reference does not change. However, you can still use the reference to change the object that it references, that is why your Vector was feeling side-effects. Consider the following:
[This message has been edited by JUNILU LACAR (edited March 05, 2001).]
The best ideas are the crazy ones. If you have a crazy idea and it works, it's really valuable.—Kent Beck
One more thing...The default clone() for Vector is a shallow clone. Therefore, it will make a copy of each reference the Vector contains. If the calling code retains a reference to one of these objects, they can still change the object referred to from the Vector even though it is cloned. I'm not sure exactly what you are doing, but you probably need a deep clone of the Vector. If you need examples, I can point you to some. Peter Haggar ------------------ Senior Software Engineer, IBM author of: Practical Java