Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Vector passed by value or by referance???

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

------------------
Hope This Helps:)
Carl Trusiak
 
Sheriff
Posts: 15815
264
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

HTH
J.Lacar

[This message has been edited by JUNILU LACAR (edited March 05, 2001).]
 
David Shepherd
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much guys. That clears it up for me.
 
author
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
brevity is the soul of wit - shakepeare. Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic