File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

object cloning

 
Himanshu Gupt
Greenhorn
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1183
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so this is what your Library class looks like?
and you want to clone that object, and every attribute should be cloned also ?

 
Himanshu Gupt
Greenhorn
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1183
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic