Post details: Java "copy constructor"

Jun 01, 2006 : Java "copy constructor"

Well, of course you can just Foo myFoo = (Foo) theirFoo.clone(); to copy an object.

But I want to create an ExtendedFoo out of a Foo. I can't cast the foo (or cloned foo) into an ExtendedFoo because it's not one. I need to create a new ExtendedFoo and copy all of the important stuff from the "foo" object into the "extendedFoo" object. Here's how I did it:

import org.apache.commons.beanutils.BeanUtils;

public class ExtendedFoo extends Foo {

	public ExtendedFoo(Foo source) throws IllegalAccessException, InvocationTargetException {
		BeanUtils.copyProperties(this, source);
	}
	...
}

Isn't that easy?

Comments, Pingbacks:

No Comments/Pingbacks for this post yet...

Leave a comment:

Your email address will not be displayed on this site.
Your URL will be displayed.
What is the 2-letter abbreviation for Michigan?
Allowed XHTML tags: <p, ul, ol, li, dl, dt, dd, address, blockquote, ins, del, span, bdo, br, em, strong, dfn, code, samp, kdb, var, cite, abbr, acronym, q, sub, sup, tt, i, b, big, small>
Options:
 
(Line breaks become <br />)
(Set cookies for name, email & url)

<  June 2019  >
Mon Tue Wed Thu Fri Sat Sun
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30

Categories


Archives

Misc

Syndicate this blog XML

powered by
b2evolution