What is the difference between cloning the object with .clone() method and = sign?
clone, cloning, java, methods
Solution
If you create a new Dog:
Dog a = new Dog("Mike");
and then:
Dog b = a;
You'd have one `Dog` and two variables that reference the same `Dog`. Therefore doing:
a.putHatOnHead("Fedora");
if (b.hasHatOnHead()) {
System.out.println("Has a hat: " + b.getHatName());
}
Will print that the dog has a Fedora hat, because `a` and `b` reference the same dog.
Instead, doing:
Dog b = a.clone();
Now you have two dogs clones. If you put a hat on each dog:
a.putHatOnHead("Rayden");
b.putHatOnHead("Fedora");
Each dog will have its own hat.
Problem
I am really confused about what is the difference between `.clone()` method or simply putting the `=` sign between objects while trying to clone it. Thank You.