XML Feeds

.

[java-dev] Re: perils of referencing

Ritchie Argue maxmspjit at gmail.com
Thu Feb 7 16:27:11 MST 2008


You should grab Effective Java book that Oliver mentioned. It has some  
good tips in this area, including defensive copying. Its a good habit  
to let the callee do the copying, as you can't always control who the  
caller is (for example if you're writing a library that provides your  
Things to third party clients, they may accidentally do something  
malicious to it (as you have perhaps discovered)).

Worry about efficiency when it becomes a problem, make sure it's  
correct first =)

r.

On 7-Feb-08, at 1342, jbmaxwell wrote:

>
> Oh yeah, jeepers... You're right. A deep copy would copy all the  
> Things in the ArrayList, as well, wouldn't it! I just want a copy of  
> the ArrayList itself. Good point.
>
> I don't know if I'll change my accessor method, but I get the idea.
>
> Is it possible to do something like:
>
> ArrayList theseThings = bigThing.getThings().clone();
>
> That would leave my accessor method getThings() untouched, and a bit  
> more efficient for most situations.
>
> J.
>
> --------------
>
> Thanks Ollie.
>
> It does make sense to me, actually, but I like to get confirmation  
> on things before I commit them to the "learned" pile... Passing  
> references around is generally really handy, so I suppose it's not  
> fair of me to complain about it now! ;-)
>
> cheers.
>
>
> _______________________________________________
> 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