Erik, Thanks for the lesson - and I'll get out and get a copy of Josh's book. In the meantime I've also discovered that 'clone' basically does a shallow copy - i.e. if there is a nested construct of javabeans, clone will only copy the hightest level, and then place reference pointers to the nested beans. I think this is what is getting me. Does anyone have a 'deep copy' class that will clone a complex javabean construct?
Jerry Jalenak Team Lead, Web Publishing LabOne, Inc. 10101 Renner Blvd. Lenexa, KS 66219 (913) 577-1496 [EMAIL PROTECTED] -----Original Message----- From: Erik Price [mailto:[EMAIL PROTECTED] Sent: Thursday, June 12, 2003 2:12 PM To: Struts Users Mailing List Subject: Re: Cloning Form Beans Erik Price wrote: > But you're still not safe yet. > > What if somehow someone had held onto a reference to one of the Date > objects passed into the constructor? > > Date myEvilStart = new Date(); > Date end = new Date(); > Period p = new Period(myEvilStart, end); > // at this point is "p" immutable? NO! > myEvilStart.setYear(78); > // The "start" field in "p" has just been modified! Whoops class, sorry, I forgot to finish the lesson. Bloch teaches us that we should also make an internal copy of each argument to the constructor: public Period(Date start, Date end) { this.start = new Date(start); this.end = new Date(end); } This way if someone does still have a reference to the argument, they can't modify that as a way into our immutable Period class. Okay class is dimissed. Erik --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] This transmission (and any information attached to it) may be confidential and is intended solely for the use of the individual or entity to which it is addressed. If you are not the intended recipient or the person responsible for delivering the transmission to the intended recipient, be advised that you have received this transmission in error and that any use, dissemination, forwarding, printing, or copying of this information is strictly prohibited. If you have received this transmission in error, please immediately notify LabOne at the following email address: [EMAIL PROTECTED] --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]