Solved

How to clone an object complettly?

Posted on 2006-11-02
1
215 Views
Last Modified: 2010-03-31
Assumed I have a class which contains elements of other classes as follow:

public ClassB{
...
}

public ClassA{
...
  private String str1;
  private B b;
...
}

I can clone the A as follow:
...
  A a = new A();
  A c = a.clone();

But this will just clone the member str1.  How can I clone the A complettly?
0
Comment
Question by:chenwei
1 Comment
 
LVL 24

Accepted Solution

by:
Tomas Helgi Johannsson earned 30 total points
Comment Utility
   Hi!

What you have to do is override the .clone() method as described in the API
By convention, classes that implement this interface should override Object.clone (which is protected) with a public method. See Object.clone() for details on overriding this method.  http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Cloneable.html
and http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Object.html#clone()
And here are some examples : http://www.java2s.com/Code/Java/Language-Basics/Clone.htm


Regards,
  Tomas Helgi
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
pairstar challenge 2 41
mapBully challenge 6 88
topping2 challenge 13 56
Core Java. What output will be and why ? 6 58
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now