Object Cloning in Java

The usual Object Cloning ways work just as well in Java. However, clone() method is much easier and can be used to overcome the limitations of those methods.

A obj1 = (A) obj.clone();

clone is a member function of Object which throws an exception when used. Why?

Cloning a class is not allowed by default for security reasons. But if the class itself allows you i.e. the class implements Cloneable, then you can use clone() method.


Backlinks