This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Originally posted by omar bili: Hi the clone method: what does it do? is it another reference to the same class, if yes how can i really clone an object, so i can have 2 different objects? Best
In Java, we don't have references to classes. We have references to objects (instances of classes) instead. The clone() method returns a new reference to a new object (a clone). However, there are some details that are discussed in the web page given by Avisesh's link above. I'll let you read that before I go on.
Please come back with more questions if you run into problems.
Hi, What i need to know, if i clone an instance of a class
Box b = new Box() ;
b.height = 100 ;
Box a = b.clone() ; a.height = 200 ;
here b.height = ?? is it 100 or 200
a and b refer to the same istance, any changes in a will be applied to b, or a and b are totally different instances, in the memory? Kind regards, Bili
Joined: Mar 16, 2005
"Object's implementation of the clone method creates an object of the same type as the original object and initializes the new object's member variables to have the same values as the original object's corresponding member variables"
I hope this will clear your doubts. [ March 22, 2005: Message edited by: Avisesh Jain ]