Can't access two vectors independantly?
Hi I haven't used Vectors before but I can't work out what I'm doing wrong here. I have a class called PointSequence which contains a Vector of Points (and some methods to work with them). If I then create two instances of this class (or an array of two for that matter), adding something to one of them seems to add it to both.
Code:
// A class for a sequence of Points
import java.awt.*;
import java.util.*;
public class PointSequence
{
static Vector <Point> points=new Vector <Point> (); //the x,y locations are stored in a vector which will grow as you add items
//i have some other methods here
public static void main(String[] args)
{
//create two instances of the new class
PointSequence testA=new PointSequence();
PointSequence testB=new PointSequence();
//add something to A
testA.points.add(new Point(256,196));
//add something different to B
testB.points.add(new Point(100,100));
//print out the contents of each instance
System.out.println("A contains...");
for(int i=0;i<testA.points.size();i++)
{
System.out.println(testA.points.elementAt(i));
}
System.out.println("B contains...");
for(int i=0;i<testB.points.size();i++)
{
System.out.println(testB.points.elementAt(i));
}
}
}
This produces the following output:
A contains...
java.awt.Point[x=256,y=196]
java.awt.Point[x=100,y=100]
B contains...
java.awt.Point[x=256,y=196]
java.awt.Point[x=100,y=100]
Why do both have the same contents even though I added one item to each?!! Is there something fundamental I am misunderstanding about Vectors or classes?
Cheers:confused: