PSilo
PSilo

Reputation: 39

Java, multiple instances of an class resets with latest created instance

I got a linked list with a class I created but every time I add a new instance to the list all other elements in the list gets the same data.

Here is the class I have in my list.

public class ZitEntity 
{
private int mSize;
public boolean poped;
private boolean done;
private Canvas can;
private int count;
private  static Vector2D mPosition;
private int ms;
private Bitmap Zit;

public ZitEntity(int a, int b)
{
    mSize = 20;
    poped = false;
    done = true;
    ms = 3;
    mPosition = new Vector2D(a,b);
    Zit = Bitmap.createBitmap(mSize*2, mSize*2, Bitmap.Config.ARGB_8888);
}

In the second class I create a list.

private LinkedList<ZitEntity> entityList;

and in the constructor i initiate the list.

entityList = new LinkedList<ZitEntity>();

And here is where I add a new instance to the list:

private void createEntity()
{
    Random generator = new Random();
    int a = generator.nextInt(10);
    if(a==5)
    {   
        int x = generator.nextInt(ZitGame.height-60)+20;
        int y = generator.nextInt(ZitGame.width-120)+20;
        entityList.add(new ZitEntity(x,y));
        Log.v("add entity", "new entity");
    }
}

Upvotes: 0

Views: 234

Answers (1)

Mahdi Hijazi
Mahdi Hijazi

Reputation: 4454

you are declaring the mPosition as static, so every time you will get the latest position for each item

Upvotes: 3

Related Questions