I ran across this problem, which has been driving me nuts. In a nutshell, I instantiate two objects of the same class. When I run a method in one object, the other object is affected too as if I called a method on a 2nd object explicitly. I was wondering if someone could please give me a hand on this.
Suppose, I have class Portfolio
...
public class Portfolio implements Cloneable {
public ArrayList<Instrument> portfolio;
private String name;
private String description;
public Portfolio() {
portfolio = new ArrayList<Instrument>();
}
public Portfolio(Portfolio copyFrom) {
//attempt to copy the object by value
this.portfolio = (ArrayList<Instrument>) copyFrom.portfolio.clone();
this.name = copyFrom.name;
this.description = copyFrom.description;
}
public void applyStress(Stress stress) {
this.portfolio.set(0,this.portfolio.get(0)+1;
}
1st constructor is used to instantiate an object, etc. 2nd constructor is used to copy an object by value.
A method applyStress
is used to run through sum calculations. in our case I simplified the method, so that it does nothing but adds +1 to whatever is in the object.
So I would instantiate an object as
Portfolio p = new Portfolio();
then I would assign to a portfolio
field, some instruments;
p.portfolio = someInstrumentList;
then I would copy by value the portfolio p
into pCopy
:
Portfolio pCopy = new Portfolio(p);
So at this time I am have two objects that are the same. Further one is a copy-by-value object. Changing values of fields in pCopy
does not affect same fields in p
.
Now, when I run a method applyStress
on p
, then the values of the instrument list in pCopy
will also change.
In other words, if p.portfolio.get(0) == 1
, then after p.applyStress
, I would expect to see that p.portfolio.get(0)
is 2
and pCopy.portfolio.get(0)
is 1
But what I see instead is p.portfolio.get(0)
is 2
and pCopy.portfolio.get(0)
is also 2
I do not understand why this happens. It is not the static
modifier issue, as there is no static modifiers. Anyone has any ideas?