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 / October 2006

Tip: Looking for answers? Try searching our database.

Beginner's Question: Create an identical object.

Thread view: 
sunbin - 03 Oct 2006 06:41 GMT
Hi all

Need help in understanding this.

Let's say I have 2 class. driver class, and customer class.

In driver class,

[code]

Customer CustA = new Customer(Name, Age);

[/code]

This will create a CustA object.
If I do this...

[code]

Customer CustB = CustA

[/code]

It is actually pointing CustB to CustA, am I right to say that CustA and
CustB is the same object in the memory?

So if yes.

[code]

Customer CustB = new Customer(CustA.getName(), CustA.getAge());

[/code]

Will it work and create another Customer object CustB with the same
attributes of CustA, can it be consider a clone of CustA?
Joan C - 03 Oct 2006 09:12 GMT
> Hi all
>
[quoted text clipped - 21 lines]
> It is actually pointing CustB to CustA, am I right to say that CustA and
> CustB is the same object in the memory?

Yes they are both referencing the same object in memory.

> So if yes.
>
> [code]
>
> Customer CustB = new Customer(CustA.getName(), CustA.getAge());

Yes, here you are creating a new object in memory - and the attributes of
this new object will have the same name and age as customer.  Note - it is
normal practise to use object names that start with a small letter to
distinguish objects from classes.  i.e. call CustB custB.

> [/code]
>
> Will it work and create another Customer object CustB with the same
> attributes of CustA, can it be consider a clone of CustA?

I guess, assuming age and name are the only attributes of these objects then
they are, for all intensive purposes, cloned.  You could also use the
Cloneable interface.
sunbin - 03 Oct 2006 17:05 GMT
Thanks joan.

Regarding the Cloneable interface, the documentation says that it's not a
guarantee cloneable if a class implements it.

So is there any condition where the Cloneable interface wont work? that you
know of?
Joan C - 03 Oct 2006 17:47 GMT
> Thanks joan.
>
[quoted text clipped - 3 lines]
> So is there any condition where the Cloneable interface wont work? that
> you know of?

As far as I am aware if a class implements the Cloneable interface then it
should be cloneable.  Please supply a link to where you read there's no
guarantee that it will work.
sunbin - 03 Oct 2006 20:43 GMT
Well I saw it on this page.
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Cloneable.html
The last paragraph.
Joan C - 03 Oct 2006 23:03 GMT
> Well I saw it on this page.
> http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Cloneable.html
> The last paragraph.

Oh fair enough ... I guess there must be occasions when you cannot clone an
object then.  Don't know enough about cloning to tell you why that would be.
Lew - 04 Oct 2006 04:41 GMT
> "sunbin" <noSpam@noSpam.com> wrote in message
>> Well I saw it on this page.
>> http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Cloneable.html
>> The last paragraph.

> Oh fair enough ... I guess there must be occasions when you cannot clone an
> object then.  Don't know enough about cloning to tell you why that would be.

Joshua Bloch's book "Effective Java" has a section on clone() - the main
difficulty is in getting a deep copy, and in the fact that the Cloneable
interface does not define the clone() method.  It is a "marker" interface, so
an implementing class might end up not really copying everything all the way
down if it only uses the default Object.clone() method.

Another part is that the Object.clone() method has only protected access, so a
client class might not be able to access it even if the cloned object
implements Cloneable.

It's a subtle topic.  Joshua Bloch suggests a copy constructor as a possible
alternative, among other ideas.  I'm still assimilating the wisdom.

- Lew


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.