views:

137

answers:

2

As I was advised by PMD, I want to reduce coopling by using interfaces instead of implementation ...

In this case, knowing that I need a cloneable param, can I overcome the clone Dilemma (no clone() method in the Cloneable interface) ??

public MyConstructor(ArrayList<E> myParam) {
    this.myAttribute = (ArrayList<E>) myParam.clone();
}
+3  A: 

You don't need to clone that way; I'd do it like this:

public MyConstructor(List<E> myParam) 
{
    this.myAttribute = new ArrayList<E>(myParam);
}
duffymo
Note that this produces an ArrayList that will allocate enough space for 110% the size of the provided List. So, it's not exactly a clone.
Allain Lalonde
True, but I think it serves the purpose.
duffymo
wj
+1  A: 

I don't know PMD well, but this would be a shallow copy, instead of deep copy.

Sarstime