java - Creating Multiple Objects in a Loop Issue -


i need create multiple objects in loop. read elsewhere adding them list accomplish task, below code gives me set of copies of same object, i.e. same values. idea how can create multiple objects, rather copies of same one? thank you.

(the code below simplified version of i'm working on)

    system.out.println("creating swarm of size "+swarmsize);     list<dog> mydogs = new arraylist<dog>();      for(int = 0; < dogamount; i++) {         system.out.println("new dog # "+i);         mydogs.add(new dog(i));     }      dog first = mydogs.get(0);     dog other = mydogs.get(3);     system.out.println(first.getid()+" "+other.getid());     //prints out number of dogs should have created -1 both times 

my dog class

import java.util.*;  public class dog{     public static int dogid;      public dog(int id) {         dogid = id;     }      public int getid() {     return dogid;     }      public void setid(int id) {         dogid = id;          } } 

change

public static int dogid; 

to

public int dogid; 

or better yet,

private int dogid; 

since have getter function it, other classes don't need access field directly.


Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -