Re: Creating an object during runtime
- From: Peter Jones <ant_on_line@xxxxxxxxxxxxxxxxxxxxxx>
- Date: Tue, 29 Nov 2005 23:10:14 +0000 (UTC)
zero wrote:
> If you want to create objects at runtime you need the reflection api.
> For example, to create a new object of class C2:
>
> Class classDefinition = Class.forName("C2");
> Object object = classDefinition.newInstance();
>
> Here object will be an instance of C2.
Allow me to attempt to clarify. I'm trying to make an object of type class,
during runtime. The problem is, that I need to make the objectname
automatically increment as the objects are created.
e.g.
C1 C2 C3 C4 etc.
(list of created objects..)
However, I cant use a variable as a parameter in the constructor, as it uses
the variable name as the object name, not the contents of the variable.
e.g.
car = C1;
vehicle car = new vehicle();
In the above line, the new object I have created is "car" and not C1. I need
it to be C1. Your reference to the reflection API has been helpful, but I
dont see where I can use a variable of somekind, or other similar
mechanism, to let me create objects with a new object name, incremented
automatically.
again, Many Thanks in advance
.
- Follow-Ups:
- Re: Creating an object during runtime
- From: zero
- Re: Creating an object during runtime
- References:
- Creating an object during runtime
- From: Peter Jones
- Re: Creating an object during runtime
- From: zero
- Creating an object during runtime
- Prev by Date: Re: A good text editor for JAVA?
- Next by Date: Proposed DB tool
- Previous by thread: Re: Creating an object during runtime
- Next by thread: Re: Creating an object during runtime
- Index(es):