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
Solved

How to clone an object complettly?

Posted on 2006-11-02
1
218 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 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Detect Closed Loops (circles, figure-8s, etc) in PNG Images 6 57
Adding multiple JVM environments to RedHat 6 7 48
Java: anonymous class 4 29
Html Table Looping (part 2) 5 27
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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 will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

860 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