Académique Documents
Professionnel Documents
Culture Documents
[Document subtitle]
Khushi
[COMPANY NAME]
Contents
Task 3: ........................................................................................................................................ 2
Java clone mechanism: ........................................................................................................... 2
why you think the Java language requires the cloning mechanism. ...................................... 2
Task 3:
*Java clone mechanism:
Java clone mechanism: In Java clone is used to create a copy of an object by using clone ().
The clone is a complicated method from java. lang. Object class and this is used to create a
copy of an object in java.
*why do we require cloning mechanism in java language?
If we want the exact copy of an object, then clone method is used Clone (). It also saves times
to create same copy of the object and if we do it by using the new keyword then it takes a lot
of time to process so, that’s by we use object Cloning.
Basically, we can do something that java language can’t do means it can copy same state of the
object, plus its real class place. The cloning mechanism in java based on each class calling the
superclass's clone method.
The Syntax for calling a clone method in java is:
My Class copy = (My Class) obj. clone ();
Any changes in obj2 will reflect on obj1 but any changes in obj1 will not reflect in obj2.
But if we will write obj2 = obj1.clone(); any changes in obj2 will reflect on obj1 and vice
versa.
If we mentioned obj2 =obj1.clone(); then, any changes occur in obj2 then it automatically
reflects in obj1 same for obj2.
Reference:
https://www.google.com/search?rlz=1C1CHBF_enNZ824NZ824&ei=NNKhXJCiGYuf9QPYurzgDg&q=ja
va+clone+mechanism&oq=java+clone+mechanism&gs_l=psy-
ab.3..0i71l8.0.0..12794...0.0..0.0.0.......0......gws-wiz.O41cUtNmsss
https://en.wikipedia.org/wiki/Clone_(Java_method)
https://www.youth4work.com/Talent/Core-Java/Forum/109211-what-is-use-of-object-cloning
https://dzone.com/articles/java-cloning-with-example