Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / March 2004

Tip: Looking for answers? Try searching our database.

'Dynamic' object creation?

Thread view: 
Bill - 31 Mar 2004 17:34 GMT
I want to use a txt file with a list of ticker symbols and create a
'stock' object for each symbol on the list.  I know how to do the file
i/o, but how can I 'dynamically' create the stock objects, each named
from a line of the txt file?  

If anyone could point me in the right direction I'd appreciate it.
Eric Sosman - 31 Mar 2004 18:23 GMT
> I want to use a txt file with a list of ticker symbols and create a
> 'stock' object for each symbol on the list.  I know how to do the file
> i/o, but how can I 'dynamically' create the stock objects, each named
> from a line of the txt file?
>
> If anyone could point me in the right direction I'd appreciate it.

   What do you mean by the "name" of your object?  If you
mean an instance member, e.g.

    class Stock {
       String name;
       ...
    }

... then just supply the name via whatever mechanism you've
already established: a Stock constructor, or a setName method,
or whatever.

   If you mean the identifier of the reference variable with
which you refer to a Stock object

    Stock bluechip = new Stock("WMT");

... then there's no way[*] to create a new `bluechip' at run
time based on the content of some file.  Besides, it's misleading
to think of `bluechip' as the "name" of the Stock; `bluechip' is
just a reference variable that might point to your Wal-Mart
stock at one moment and to your Enron stock the next.  And it
mightn't even be unique: What if you follow the above with

    Stock redchip = bluechip;
    Stock greenchip = redchip;
    Stock tacochip = greenchip;

?  It's clearly nonsensical to say that all of `bluechip',
`redchip', `greenchip', and `tacochip' are "the name" of your
Wal-Mart stock.

  [*] Actually, there *is* a way to create new reference
variables at run time, but you must give up some freedom in
the way you name them.  Here's how:

    int stocks_count = 42;  // or figure it out somehow
    Stock[] stocks = new Stock[stocks_count];

... and now you've got a whole bunch of references, named
`stocks[0]', `stocks[1]', and so on.  Or instead of an array
you could use a Collection object to hold "nameless" references
to your Stock objects:

    List stocks = new ArrayList();
    stocks.add(new Stock("WMT"));
    stocks.add(new Stock("IBM"));
    ...

Signature

Eric.Sosman@sun.com



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.