> 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