In the first case you are instantiating a new object and store the reference in e1.
suppose you are changing the salary in this next step e1.setSalary(10000);
then if e1
object is cloned,
Employee e2 = (Employee)e1.clone();
a new instance with salary as current value stored in e1 will be saved.
basically e2
will be a new instance of Employee Class and whose salary is 10000
if you later change the salary of e2
it won't affect e1
.
e.g.. e2.setSalary(2000);
then salary in e1 would remain the same e1 -> 10000 whereas for e2 -> 2000
In the third step, you are referencing e1 to e2,i.e. e2 would point to an instance which was referenced by e1, e1 and e2 would point to same instance... so the cloned object will be like object without reference which is eligible for gc to act upon
For getting to know more about clone, you could refer this post https://stackoverflow.com/a/9834683