I am really confused by what constitutes the difference between a method .clone()or simply by placing a sign =between objects when trying to clone it.
.clone()
=
Thank.
If you create a new dog:
Dog a = new Dog("Mike");
and then:
Dog b = a;
You will have one Dogand two variables that reference the same Dog. Therefore:
Dog
a.putHatOnHead("Fedora"); if (b.hasHatOnHead()) { System.out.println("Has a hat: " + b.getHatName()); }
Displays that the dog has the Fedora hat, because it aand brefer to the same dog.
a
b
Instead, run:
Dog b = a.clone();
You now have two dog clones. If you put a hat on each dog:
a.putHatOnHead("Rayden"); b.putHatOnHead("Fedora");
.
:
Object obj = new Object(); //creates a new object on the heap and links the reference obj to that object
1:
Object obj2 = obj; //there is only one object on the heap but now two references are pointing to it.
2:
Object obj2 = obj.clone(); //creates a new object on the heap, with same variables and values contained in obj and links the reference obj2 to it.
, java api
= - java. a = b " a b. b - , a = b a , b. .
a = b
, (), , Clonable, clone().
Clonable
clone()
clone() "" , clone() , , , , , .
= . .clone , .
.clone
= . clone() ,