• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Re-creation of object

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
How can I re-create a object of a class where the object is already created.

Suppose, I have a class called Test.java
Already I have created a object test1 of that class.Now I want to re-create the same object with same state .How many ways can I achieve that?

Regards,
Sahid
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1) add a copy constructor to the class. This constructor then copies the state of the passed object to its own state.

2) override Object.clone().

In both cases you need to be aware that simply copying all your fields will share data. If your fields are not primitives or immutable (like String, the wrapper classes) you should copy those too.

An example, using a copy constructor:
 
reply
    Bookmark Topic Watch Topic
  • New Topic