Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / April 2005

Tip: Looking for answers? Try searching our database.

returning a deep copy of an object

Thread view: 
Shea Martin - 07 Apr 2005 16:01 GMT
This seems elementary, but I must be missing something:
How to I return a deep copy of an object?  For example the following code
looks legit to me, but the compiler complains that clone() has protected
access in java.lang.Object.

class DB
{
  private BigInteger serialCounter;
  public DB() { serialCounter = new BigInteger(); }

  private synchronized BigInteger getNextSerialValue()
  {
    serialCounter = serialCounter.add( BigInteger.ONE );
    return serialCounter.clone();
  }
}

~S
Eric Sosman - 07 Apr 2005 16:49 GMT
> This seems elementary, but I must be missing something:
> How to I return a deep copy of an object?  For example the following code
[quoted text clipped - 12 lines]
>    }
> }

   Roughly speaking, you can only clone an object with the
object's cooperation.  The object indicates its willingness
to cooperate by implementing the Cloneable interface (which
BigInteger does not).

   Instead of trying to clone the BigInteger, you could
construct a new one with the same value:

    return new BigInteger(serialCounter.toByteArray());

   However, all this copying seems pretty pointless.  The
BigInteger object is immutable, so its value won't change
after it's created.  When you "increment" serialCounter
you are not changing the value of an existing BigInteger;
rather, add() gives you a fresh BigInteger with the new
value.  Thus, you can simply

    return serialCounter;

without all the bother.  (With this in mind, note that you
could initialize serialCounter to BigInteger.ZERO instead
of creating a redundant zero-valued object; a zero is a
zero is a zero.)

Signature

Eric.Sosman@sun.com

sanjay manohar - 07 Apr 2005 16:49 GMT
BigIntegers are immutable. Why would you need a clone?
PS what does the constructor "new BigInteger()" create? perhaps you
meant BigInteger.ZERO.
Shea Martin - 07 Apr 2005 20:26 GMT
> This seems elementary, but I must be missing something:
> How to I return a deep copy of an object?  For example the following
[quoted text clipped - 14 lines]
>
> ~S

Immutable.  I get it.

Thanks,

~S


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.