is it me or does the get() method return the address of an object instead of a copy of an object
like for example
public class z
{
public static void main(String[] args)
{
ArrayList list = new ArrayList();
list.add(new Bob());
System.out.println("Before Change");
System.out.println( ( (Bob)list.get(0) ).getX() );
Bob testObject = (Bob)list.get(0);
testObject.a();
System.out.println("After Change");
System.out.println( ( (Bob)list.get(0) ).getX() );
}
}
class Bob
{
private int x =0;
public Bob()
{
}
public void a()
{
x++;
}
public int getX()
{
return x;
}
}
Considering this code, if testObject is only a copy of whats in get(0), then if i do anything to testObject, wouldn’t the element in get(0) stay the same??? but when i execute the code, the element at get(0) becomes changed when I didn’t use any set methods at all.
Another example with iterator.
public class test
{
ArrayList a = new ArrayList();
public test()
{
a.add(new bob());
}
public ArrayList getList()
{
return a;
}
}
class Bobtester
{
public static void main(String[] args)
{
test t = new test();
ArrayList b = t.getList();
Iterator bi = b.iterator();
while(bi.hasNext())
{
bob c = (bob)bi.next();
}
System.out.println(b);
System.out.println(t.getList());
}
}
class bob
{
int x=0;
public void at()
{
x++;
}
public String toString()
{
return Integer.toString(x);
}
}
Basically ArrayList b is suppose to be a copy of ArrayList a, but when I remove each element from ArrayList b, each element is also removed from ArrayList a…why is that???
