> 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