June 2004
Intermediate to advanced
848 pages
21h 28m
English
When we looked at the Object class in Chapter 3, we saw that it has this method:
protected native Object clone() throws CloneNotSupportedException;
Java supports the notion of cloning, meaning to get a complete bit-for-bit duplicate of an object. Cloning, perhaps surprisingly, does not invoke any constructor.
Not every class should support the ability to clone. If I have a class that represents a set of objects that are unique in the real world, such as the ships in a company fleet, the operation of cloning doesn't make sense. You buy new ships, but you can't get exact duplicates in every detail (including, say, registration number) of an existing ship. However, methods (including the ...
Read now
Unlock full access