XML Feeds

.

[java-dev] perils of referencing

jbmaxwell jbmaxwell at rubato-music.com
Thu Feb 7 12:59:23 MST 2008


Hello All,

I need to clear something up...

Suppose I have two classes: ComplexThing, and Thing.

ComplexThing is a composite object, made up of Things, which has some special powers as a result (night vision, omnipotence, etc.). It has a simple variable, Things, which is an ArrayList<Thing>, and holds all Things that make up ComplexThing.

ComplexThing has a method, getThings(), which returns all the Things from which it's made:

ArrayList<Thing> getThings()     {return Things;}

I have another class, Thingulator, which operates on ComplexThings.

if Thingulator does something like:

ComplexThing bigThing;
Thing aThing;

ArrayList<Thing> theseThings = bigThing.getThings();

if(theseThings.contains(aThing))
   theseThings.remove(aThing);

Obviously, aThing gets removed from theseThings, but does it also get removed from ComplexThings.Things? I have a sinking feeling it does, as this makes perfect sense, given the whole idea of referencing, but that would also kind of suck if it did. I think there are a few places where I've done this sort of "removal", without realizing how far-reaching the references are...

Also, if my fears are well-founded, is the only way around it a deep copy of ComplexThings.Things? I'm guessing so, but that also kind of sucks, as deep copies in Java are such a pain (why has this not been made simpler, over the years?).

Any clarification welcome.

cheers,

J.




More information about the java-dev mailing list