aspose file tools*
The moose likes Java in General and the fly likes object cloning Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "object cloning" Watch "object cloning" New topic
Author

object cloning

Himanshu Gupt
Greenhorn

Joined: Dec 03, 2006
Posts: 13
Hi,

I have a complex object, it contains lots of attributes and associations

for example

my object is library it has few attributes called name, location etc, and it is associated with books and books is associated with pages and pages associated with words.


now i want to create a new object of library with same attribute values and i want to set id as null for all associated object, so that i can regenerate all those ids.

i hope,i was able to convey my question.

Thanks
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
so this is what your Library class looks like?
and you want to clone that object, and every attribute should be cloned also ?



JDBCSupport - An easy to use, light-weight JDBC framework -
Himanshu Gupt
Greenhorn

Joined: Dec 03, 2006
Posts: 13
My code is like this



and I want to copy whole structure without id attribute from all objects.
I hope this makes my question more clear.

Thanks
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
make your Librar Cloneable by implementing the Cloneable marker interface ..

override the Object's clone method and change the access modifier to public ..

in that method call super.clone();

now it depends on whether or not you want a shallow or deep clone ..

shallow clone uses the same instance of your instance variables and assigns it to the cloned Library object.
Deep Clone also clones your instance variables by invoking their clone method. Hence also these have to be Cloneable.

check here http://www.go4expert.com/forums/showthread.php?t=5424
 
jQuery in Action, 2nd edition
 
subject: object cloning