Re: Object copies?
On 7/31/2011 4:38 PM, Knute Johnson wrote:
On 7/31/2011 4:26 PM, Patricia Shanahan wrote:
FindBugs is probably complaining because you probably do not need to
copy. Remember that none of the String methods modifies the String on
which it is called.
Light Bulb! All I need is another set of references.
Yeah, I was going to say the same as Patricia. Strings are immutable,
always. Why do you need to make a copy?
Also, what is this for? Some sort of before-and-after object, like a
Command that can be reverted? Just save the original and the new:
class StringModification implements Command {
private final String before, after;
public StringModification( String before, String after ) {
this.before = before; this.after = after;
}
public String revert() { return before; }
}
Invoke with:
new StringModification( str, str.replace(...) );
or similar. Actually for this particular application, all you probably
need to save is the "before" bit, but you did ask for a copy of both.