Re: Noob question - StringBuffer
fxtrad@gmail.com wrote:
So it seems to me that the only way we can pass a true deep-copy of an
object as an argument, is to override the clone() method and
EXPLICITLY invoke it like so: operate((Simple)a.clone(),
(Simple)b.clone());
I thought your question was about pass-by-value vs. pass-by-reference for
method arguments. Now you're talking about deep cloning, which is an entirely
separate topic and doesn't really cross over.
This is mildly confusing to me.
class Simple implements Cloneable
How come the class isn't public? It doesn't really matter, I'm just curious.
{
private Vector<Object> inner;
Are you sure you need Vector? Why not ArrayList?
You should reconsider your generic argument - you apparently want some sort of
List <String>, so why did you declare it as <Object>?
public Simple(String str)
{
inner = new Vector();
You shouldn't mix generics and raw types. Didn't the compiler issue you a
warning?
inner.add(str);
}
public void addInner(Simple s) {inner.add(s.toString());}
You should indent more conventionally. This is an unusual idiom - having an
instance that holds Strings hold its own String representation, which in turn
public String toString() {return inner.toString();}
comes from that very holder, using the Java default Object.toString(), which
returns an obscure String containing hex digits and other stuff.
What is the ultimate point of all that circularity?
public Object clone()
{ try{ //performing the "deep" copying here:
Simple simple = new Simple(inner.toString());
return simple;
}catch (Exception e){
e.printStackTrace();
return this;
}
}
}
This clone() method does not return anything like a copy of the original
object, and therefore doesn't live up to the purpose of clone().
--
Lew