Getting the references of all the instantiated classes in my program

Discussion in 'Java' started by D Sandher, Sep 24, 2003.

  1. D Sandher

    D Sandher Guest

    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
     
    D Sandher, Sep 24, 2003
    #1
    1. Advertising

  2. Re: Getting the references of all the instantiated classes in myprogram

    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};
     
    Michael Borgwardt, Sep 24, 2003
    #2
    1. Advertising

  3. D Sandher

    Chris Smith Guest

    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
     
    Chris Smith, Sep 24, 2003
    #3
  4. D Sandher

    Tom McGlynn Guest

    Michael Borgwardt <> wrote in message news:<bksf8v$5bujp$-berlin.de>...
    > 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
     
    Tom McGlynn, Sep 24, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    372
  2. =?Utf-8?B?cGlyaG8=?=

    web pages, instantiated classes, and parents

    =?Utf-8?B?cGlyaG8=?=, Nov 17, 2006, in forum: ASP .Net
    Replies:
    8
    Views:
    324
    =?Utf-8?B?cGlyaG8=?=
    Nov 20, 2006
  3. Peter Parker
    Replies:
    3
    Views:
    895
  4. Justin
    Replies:
    5
    Views:
    510
    Wesley Hall
    Nov 2, 2006
  5. Jack Bates
    Replies:
    3
    Views:
    314
    Steven D'Aprano
    Aug 6, 2011
Loading...

Share This Page