Re: Instantiate a class with a variable as its name



BogusException wrote:
The subject line says it all! This is an extension of a previous topic.

I want to create an instance of a class, but want to refer to the name
of the instance, at creation time, via a variable. So, if I were to
make 3 classes in an iterative loop, I would be able to call the
instances c1, c2, c3 programmatically.

The equivelent is close to anonymous hashes and arrays in Perl, like
$$someArrayName[0];
.. OK, so its not that close!

The result is that each of the instantiated classes can then also be
addressed programmatically-without knowing their exact instance name.
So if I knew that I created 3 classes (which I would, as they were
created iteratively), I could call a method common to all 3 one at a
time.

All of the Java documentation regarding anonymity refers to inner
classes, which although powerful are not the same thing as an anonymous
name for a class instance. Is this possible in Java?

Sounds to me like you want a List (or array)...

How does what you want differ from:

List<C> cList = new ArrayList<C>();
for (int ct=0; ct<num; ++ct) {
cList.add(new C());
}
....
for (C c : cList) {
c.someMethod();
}

?

Tom Hawtin
--
Unemployed English Java programmer
http://jroller.com/page/tackline/
.



Relevant Pages

  • Re: Instantiate a class with a variable as its name
    ... I want to create an instance of a class, but want to refer to the name ... of the instance, at creation time, via a variable. ... All of the Java documentation regarding anonymity refers to inner ...
    (comp.lang.java.programmer)
  • Re: silly unsigned/signed byte conversion question
    ... >> Java has no such type as an unsigned byte. ... i was misleadingly trying to refer to an octet value's ... > my original problem was reading 8-bit data from a character stream and ...
    (comp.lang.java.programmer)
  • Re: silly unsigned/signed byte conversion question
    ... > Java has no such type as an unsigned byte. ... i was misleadingly trying to refer to an octet value's ... "unsigned" representation as short, int or long. ...
    (comp.lang.java.programmer)
  • How to debug EXCEPTION_ACCESS_VIOLATION?
    ... appreciate any helpfule debugging tips! ... Please refer to release documentation for possible ... # Java VM: Java HotSpotClient VM ... # An error report file has been saved as hs_err_pid4308.log. ...
    (comp.lang.java.help)
  • Instantiate a class with a variable as its name
    ... I want to create an instance of a class, but want to refer to the name ... addressed programmatically-without knowing their exact instance name. ... All of the Java documentation regarding anonymity refers to inner ...
    (comp.lang.java.programmer)