Solved

How to clone an object complettly?

Posted on 2006-11-02
1
220 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 25

Accepted Solution

by:
Tomas Helgi Johannsson earned 30 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
jboss 7.1 start up error 1 85
java imports not found 4 71
collection output issue 9 96
Where to place postgres JDBC driver jar on tomcat 8 71
In this post we will learn different types of Android Layout and some basics of an Android App.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

752 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