Re: Creating an object during runtime
- From: zero <zero@xxxxxxx>
- Date: Tue, 29 Nov 2005 22:15:36 GMT
Peter Jones <ant_on_line@xxxxxxxxxxxxxxxxxxxxxx> wrote in
news:dmiier$1ql$1@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:
> I'm trying to generate an object name during run time, and then create
> an object with the contents of that variable. You can see that I'm
> trying to also make the object name self generating (almost, the 'y'
> variable isnt being incremented yet), putting two values ('y' and
> 'C'). My apologies for poor coding structure / lack of comments, but
> I'm am a bit lost on how to work it out. MANY thanks in advance.Heres
> what I have so far:
>
> public class testVariable
Please start class names with a capital letter.
> {
> private String name = "";
> private String New_name = "";
Please don't start variable names with a capital letter.
> private int y = 2;
>
> public testVariable()
> {}
>
> public void test(String x)
> {
> name = x;
> New_name = name + y ;
> System.out.println(New_name);
> }
>
> public String getNew_name()
> {
> return New_name;
> }
>
>
>
> public static void main(String[] args)
> {
> int z = 2;
> testVariable Darren = new testVariable();
Please don't start variable names with a capital letter.
> Darren.test("C");
> testVariable "c"+ z = new testVariable();
This line can't possibly compile correctly. What are you trying to do
here?
> String Old_name = New_name;
> }
> }
>
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.
--
Beware the False Authority Syndrome
.
- Follow-Ups:
- Re: Creating an object during runtime
- From: Stefan Schulz
- Re: Creating an object during runtime
- From: Peter Jones
- Re: Creating an object during runtime
- References:
- Creating an object during runtime
- From: Peter Jones
- Creating an object during runtime
- Prev by Date: a Timer in a URLConnection
- Next by Date: Re: a Timer in a URLConnection
- Previous by thread: Re: Creating an object during runtime
- Next by thread: Re: Creating an object during runtime
- Index(es):
Relevant Pages
|
|