Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to clone an object complettly?

Posted on 2006-11-02
1
Medium Priority
?
226 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 26

Accepted Solution

by:
Tomas Helgi Johannsson earned 120 total points
ID: 17857089
   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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

971 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