XML Feeds

.

[java-dev] perils of referencing

Oliver Bown ollie at icarus.nu
Thu Feb 7 14:11:15 MST 2008


That's right. Except for primitives, everything in Java is a  
reference to an object somewhere in memory, and it's the reference  
that you're passing around. There's a book called Effective Java by  
Josh Bloch which I found quite useful on such topics, and recommended  
by many. It's very readable, and not too heavy.

Ollie

On 7 Feb 2008, at 19:59, jbmaxwell wrote:

>
> 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.
>
>
> _______________________________________________
> java-dev mailing list
> java-dev at cycling74.com
> http://www.cycling74.com/mailman/listinfo/java-dev



More information about the java-dev mailing list