Print

Print


Michael,

They don't have the 'same reference.'  The new Particle() statement
creates a new object and a new reference.  p is assigned to that
reference.  Then that object is added to the schedule via a copy of
the reference which holds a reference to that object, not p.  P is
then assigned to a new reference and object.

A reasonable explanation:
http://www.javaworld.com/javaworld/javaqa/2000-05/03-qa-0526-pass.html

Tony Bigbee


On Aug 18, 2005, at 4:11 PM, Michael Makowsky wrote:

> I am confused how the following code from Tutorial3 accomplishes
> the task of
> instantiating an assortment of particle agents:
>
> public void start()
>         {
>         super.start();
>         trails = new DoubleGrid2D(gridWidth, gridHeight);
>         particles = new SparseGrid2D(gridWidth, gridHeight);
>
>         Particle p;
>
>         for(int i=0 ; i<numParticles ; i++)
>             {
>             p = new Particle(random.nextInt(3) - 1, random.nextInt
> (3) - 1);
> schedule.scheduleRepeating(p);
>             particles.setObjectLocation(p,
>                                         new
> Int2D(random.nextInt(gridWidth),random.nextInt(gridHeight)));
>
>             }
>
> It would appear that we want to a create a collection of particles,
> but in
> fact we keep creating a single instance of the particle class (or
> at least
> multiple objects with the same reference "p." If we wanted to have the
> particles interact with each other (bounce off of each other), how
> could we
> differentiate between the particle objects, since they all have the
> same
> reference?
>
> Thanks
>
> mike
>
> Michael Makowsky
> Graduate Student
> Department of Economics, 3G4
> George Mason University
> Fairfax, VA 22030
> 703.608.1366
> [log in to unmask]
> homepage http://mason.gmu.edu/~mmakowsk
>