Pergunta

The Java documentation says:

A class implements the Cloneable interface to indicate to the Object.clone() method that it is legal for that method to make a field-for-field copy of instances of that class.

Invoking Object's clone method on an instance that does not implement the Cloneable interface results in the exception CloneNotSupportedException being thrown.

By convention, classes that implement this interface should override Object.clone (which is protected) with a public method. See Object.clone() for details on overriding this method.

Note that this interface does not contain the clone method. Therefore, it is not possible to clone an object merely by virtue of the fact that it implements this interface. Even if the clone method is invoked reflectively, there is no guarantee that it will succeed.

And I have this UserProfile class:

public class UserProfile implements Cloneable {
    private String name;
    private int ssn;
    private String address;

    public UserProfile(String name, int ssn, String address) {
        this.name = name;
        this.ssn = ssn;
        this.address = address;
    }

    public UserProfile(UserProfile user) {
        this.name = user.getName();
        this.ssn = user.getSSN();
        this.address = user.getAddress();
    }

    // get methods here...

    @Override
    public UserProfile clone() {
        return new UserProfile(this);
    }
}

And for testing porpuses, I do this in main():

UserProfile up1 = new UserProfile("User", 123, "Street");
UserProfile up2 = up1.clone();

So far, no problems compiling/running. Now, per my understanding of the documentation, removing implements Cloneable from the UserProfile class should throw an exception in up1.clone() call, but it doesn't.

I've read around here that the Cloneable interface is broken but I don't really know what that means. Am I missing something?

Foi útil?

Solução

Now, per my understanding of the documentation, removing implements Cloneable from the UserProfile class should throw and exception in up1.clone() call, but it doesn't.

As long as your class still has an implementation of the clone() method, an exception will ofcourse not be thrown when you call it - it works just like any other method, there's no special magic involved.

The implementation of clone() in class Object is what throws the exception, but you've overridden that method.

Outras dicas

It means that if you implements Cloneable and omit the clone() method and THEN call the clone() method, an exception will be thrown.

EDIT: It has been mentioned probably 1 billion times before, but

DON'T USE THE CLONE METHOD!

If you need cloning functionality, provide a copy constructor instead.

The interface is called broken because it doesn't force you to implement clone() (which it should).

I agree with both answers and add something: the interface is like a 'tag' to say that your class implements clone(). That's useful in api-like methods when you don't know the object type. Then you can write

if (myobj instanceof Cloneable) { dosmthng(); }
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top