Getting the references of all the instantiated classes in my program  
Author Message
D Sandher





PostPosted: 2003-9-24 23:55:00 Top

java-programmer, Getting the references of all the instantiated classes in my program Hi All,

I want to do something like the following: Say I have an Auctioneer and a
Bidder class and then in my program I write:

a1=new Auctioneer(blah blah blah);
b1= new Bidder(blah blah blah);
b2= new Bidder(blah blah blah);

Is there a way I can get a list of references for the objects? I would
like to have an array with a1,b1,b2 as references. Is that possible?

I look forward to hearing from you soon.

Thanks,
Dil


 
Michael Borgwardt





PostPosted: 2003-9-25 0:04:00 Top

java-programmer >> Getting the references of all the instantiated classes in my program D Sandher wrote:
> I want to do something like the following: Say I have an Auctioneer and a
> Bidder class and then in my program I write:
>
> a1=new Auctioneer(blah blah blah);
> b1= new Bidder(blah blah blah);
> b2= new Bidder(blah blah blah);
>
> Is there a way I can get a list of references for the objects? I would
> like to have an array with a1,b1,b2 as references. Is that possible?

Sure:
Object[] array = new Object[]{a1,b1,b2};

 
Chris Smith





PostPosted: 2003-9-25 0:20:00 Top

java-programmer >> Getting the references of all the instantiated classes in my program D Sandher wrote:
> I want to do something like the following: Say I have an Auctioneer and a
> Bidder class and then in my program I write:
>
> a1=new Auctioneer(blah blah blah);
> b1= new Bidder(blah blah blah);
> b2= new Bidder(blah blah blah);
>
> Is there a way I can get a list of references for the objects? I would
> like to have an array with a1,b1,b2 as references. Is that possible?

A list of all references in the application? No. You'd have to keep
track of specific references as they are created, modified, go out of
scope, etc.

Alternative, the profiling or debugging interfaces to the VM may be able
to provide this information to native code, for debugging purposes.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
 
tam





PostPosted: 2003-9-25 5:27:00 Top

java-programmer >> Getting the references of all the instantiated classes in my program Michael Borgwardt <email***@***.com> wrote in message news:<bksf8v$5bujp$email***@***.com>...
> D Sandher wrote:
> > I want to do something like the following: Say I have an Auctioneer and a
> > Bidder class and then in my program I write:
> >
> > a1=new Auctioneer(blah blah blah);
> > b1= new Bidder(blah blah blah);
> > b2= new Bidder(blah blah blah);
> >
> > Is there a way I can get a list of references for the objects? I would
> > like to have an array with a1,b1,b2 as references. Is that possible?
>
> Sure:
> Object[] array = new Object[]{a1,b1,b2};

I'm guessing the original poster wants to be able to go somewhere and
get a list of all of the Auctioneers and Bidder's that have been created.

If that's what you want, and you never want to get rid of one of these
(or you're willing to call some special destructor method when you do),
then you can have code in the constructors for these objects save
the objects in some kind of collection.

I.e., you could have code like:

[in the bidder constructor]
public Bidder(blah blah blah) {
...
collection.add(this)
...
}

Then when you want a list of all of the actors you do something
like:
actors = collection.toArray();

There are a fair number of subtleties to watch out for,
especially if you want the collection to have disparate
classes or if the code is multithreaded.

Good luck,
Tom McGlynn