Getting the references of all the instantiated classes in my program

D

D Sandher

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
 
M

Michael Borgwardt

D said:
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};
 
C

Chris Smith

D said:
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
 
T

Tom McGlynn

Michael Borgwardt said:
D said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top