Re: Creating an object during runtime



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
.



Relevant Pages

  • Re: Validation rule for 1 capital letter?
    ... > Hi all - apologies if this is a stupid question! ... > attempting to set up a Validation Rule so that the first letter in the ... > that an error message is received if the capital letter is not entered. ...
    (microsoft.public.access.tablesdbdesign)
  • Creating an object during runtime
    ... poor coding structure / lack of comments, but I'm am a bit lost on how to ... Prev by Date: ...
    (comp.lang.java.programmer)
  • Re: PING: MIS whiner
    ... Fives look like a capital letter S. Things just haven't been the same since I lost my glasses. ... My insurance only covers frames once every two years, and I have eleven months to go. ...
    (rec.arts.mystery)