Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
Question on Java Programming: How should I correctly implement and define the 'Clone' method from the 'Cloneable' interface?
So I understand that it would return an object whose instance variables are equal to the calling object's and this would work fine for instance variables whose types are immutable (String, wrapper class types, primitives, etc.) but for mutable, it opens up the possibility of privacy leaks.
So let's say an object of 'SomeClass', called "example", has 2 instance variables, one int and one of 'MutableType'.
So it'd look like
public class SomeClass{
private int number;
private MutableType type;
}
Would I do something like
SomeClass newObj = example.clone();
And then do...
newObj.setMutableType(MutableType e){
type = e.clone();
}
//Assuming that 'MutableType' has implemented the clone method correctly also to prevent privacy leaks.
I'm still relatively new to Java and confused about how to implement the clone method correctly to prevent privacy leaks. Any help would be appreciated.
1 Answer
- husoskiLv 74 years agoFavorite Answer
Make a (deep) copy of every mutable field. You can use the field's clone method, if it has one, or you can use the "new" operator to create a duplicate in many cases. Primitive data (int, double, boolean, etc.) can be simply assigned since those are always copied. Arrays of primitive or immutable types can be copied with the Arrays.copy() static methods. Arrays of mutable types must be cloned element-by-element.
You should also be doing this in every "getter" method for a mutable field. (If that's a lot of work for you, then you might have too many getters...usually meaning too much object behavior being implemented outside of the class.)
(To an OOP purist, *any* outside implementation of object behavior is too much. Encapsulation is supposed to keep all object state processing inside of the class.)
In your example, I'd suggest something like:
public class SomeType implements Cloneable {
.... private int number;
.... private MutableType mutable; // ("type" is a lousy name for a field)
... etc.
.... @Override
.... public SomeType clone() { // implementation of clone method
.... .... SomeType result = new SomeType();
.... .... result.number = this.number;
.... .... result.mutable = this.mutable.clone();
.... .... return result;
.... }
}
However, you could also have a private constructor that accepts all the relevant fields as-is, and then simply:
return new SomeType(this.number, this.mutable.clone());
The important idea is that the new object gets a "deep copy" of every mutable field.
PS: Implementing Cloneable is usually avoidable. It's a major pain with inheritance. A public "copy constructor" is almost always a better choice.