I had a doubt in my code snippet : my main class
public static void main(String[] args) {
Test test = new Test();
test.copyAtoB(3);
test.setValueinAlist(30, 3);
test.showValues(3, 0);
}
And this my dummy class
public class Test {
public ArrayList<MyClass> alist = new ArrayList<MyClass>();
public ArrayList<MyClass> blist = new ArrayList<MyClass>();
Test() {
for (int i = 0; i < 10; i++) {
MyClass myClass = new MyClass();
myClass.setCount(i);
alist.add(myClass);
}
}
public void copyAtoB(int position) {
MyClass o = alist.get(position);
System.out.println("vlaue of count in object of myclass going to be copied "+o.getCount());
blist.add(o);
}
public void setValueinAlist(int val,int position){
MyClass myClass= alist.get(position);
System.out.println(myClass.getCount()+" is changing to "+val);
myClass.setCount(val);
}
public void showValues(int aPostion,int bPosition){
System.out.println("Vlaue in a "+alist.get(aPostion).getCount()+"\n Vlaue in b "+blist.get(bPosition).getCount());
}
}
Here comes My object class
public class MyClass {
int count;
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
}
when i run my code i was expecting output like this
vlaue of count in object of myclass going to be copied 3
3 is changing to 30
Vlaue in a 30
Vlaue in b 3
But what i'm getting is this
vlaue of count in object of myclass going to be copied 3
3 is changing to 30
Vlaue in a 30
Vlaue in b 30
Could you help me to understand why my concept is wrong ?? I didn't write code to change the value in "blist" but it also get changed how does this happens ?? May be i'm asking a blunder but i couldn't resist