creating classes dynamically  
Author Message
grahamsyuk





PostPosted: 2006-3-17 23:10:00 Top

java-programmer, creating classes dynamically HI is it possible to create classes dynamically and if so how? What im
trying to do is have a user enter names into a textbox and use these
names to create instances of classes dynamically. the code below doesnt
work once never mind for numerous classes?????

String help= TextField.getText();

help = new SOP();




cheers

 
Oliver Wong





PostPosted: 2006-3-17 23:36:00 Top

java-programmer >> creating classes dynamically
"grahamsyuk" <email***@***.com> wrote in message
news:email***@***.com...
> HI is it possible to create classes dynamically and if so how?

It's possible, but complicated, and generally not as useful as it might
sound.

> What im
> trying to do is have a user enter names into a textbox and use these
> names to create instances of classes dynamically.

How does the name affect class generation? The name of the class is the
name provided perhaps? If so, why would the user care about this
"implementation detail"?

> the code below doesnt
> work once never mind for numerous classes?????
>
> String help= TextField.getText();
>
> help = new SOP();
>

Does SOP somehow extend String (even though String is declared to be
final)?

- Oliver

 
grahamsyuk





PostPosted: 2006-3-17 23:50:00 Top

java-programmer >> creating classes dynamically How does the name affect class generation? The name of the class is the
name provided perhaps? If so, why would the user care about this
"implementation detail"?

-> In essence my program is to allow a user to create multiple Standard
Operating procedures for various recipes. The program starts off empty.
I was hoping to have the user create an instance of the standard
operating procedure class each time he wishes to enter a new receipe
and be able to do this indefinitely. However, im now thinking it would
be easier to instantiate a specific number of classes at runtime and
impose a limit this way.


> the code below doesnt
> work once never mind for numerous classes?????

> String help= TextField.getText();
> help = new SOP();

Is it possible to somehow extract the value of help above to create the
class rather than using the variable name???


Does SOP somehow extend String (even though String is declared to be
final)?

No, SOP is made up of a number of vectors. Thanks for your help, i feel
im getting out of my depth. cheers

glenn

 
 
Thomas Weidenfeller





PostPosted: 2006-3-18 0:03:00 Top

java-programmer >> creating classes dynamically grahamsyuk wrote:
> HI is it possible to create classes dynamically and if so how? What im
> trying to do is have a user enter names into a textbox and use these
> names to create instances of classes dynamically. the code below doesnt
> work once never mind for numerous classes?????
>
> String help= TextField.getText();
>
> help = new SOP();

SOP? As a general hint: The easier you make it for us to understand you,
the easier it is for us to help you.

Maybe you want this, maybe you don't:

HashMap instances = new HashMap();

...

String name = ...;
instances.put(name, new Something());

...


Someting anInstance = (Something)instances.get(name);


All this has nothing to do with GUI development, so why are you asking
in a GUI group?

/Thomas

--
The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/
 
 
Oliver Wong





PostPosted: 2006-3-18 0:07:00 Top

java-programmer >> creating classes dynamically
"grahamsyuk" <email***@***.com> wrote in message
news:email***@***.com...
> How does the name affect class generation? The name of the class is the
> name provided perhaps? If so, why would the user care about this
> "implementation detail"?
>
> -> In essence my program is to allow a user to create multiple Standard
> Operating procedures for various recipes. The program starts off empty.
> I was hoping to have the user create an instance of the standard
> operating procedure class each time he wishes to enter a new receipe
> and be able to do this indefinitely. However, im now thinking it would
> be easier to instantiate a specific number of classes at runtime and
> impose a limit this way.

So why not have a class called "StandardOperatingProcedures", and create
instances of that class based on the name provided, rather than creating
whole new classes dynamically? E.g.

<pseudoCode>
String name = getUserInput();
SOP mySop = new SOP(name);
</pseudoCode>

>
>
>> the code below doesnt
>> work once never mind for numerous classes?????
>
>> String help= TextField.getText();
>> help = new SOP();
>
> Is it possible to somehow extract the value of help above to create the
> class rather than using the variable name???
>
>
> Does SOP somehow extend String (even though String is declared to be
> final)?
>
> No, SOP is made up of a number of vectors. Thanks for your help, i feel
> im getting out of my depth. cheers

The code you gave above says you're assigning to help (which is of type
String) a new instance of SOP. If SOP doesn't extend String, then you're
going to have a type error, as help can only store objects of type String,
and SOP isn't of type String.

- Oliver

 
 
Chris Smith





PostPosted: 2006-3-18 0:14:00 Top

java-programmer >> creating classes dynamically grahamsyuk <email***@***.com> wrote:
> -> In essence my program is to allow a user to create multiple Standard
> Operating procedures for various recipes. The program starts off empty.
> I was hoping to have the user create an instance of the standard
> operating procedure class each time he wishes to enter a new receipe
> and be able to do this indefinitely. However, im now thinking it would
> be easier to instantiate a specific number of classes at runtime and
> impose a limit this way.

I see no reason why you'd need to limit the user in terms of how many
objects he/she is allowed to create. I also don't see why doing this
would require creating classes dynamically. (I also wonder if you
really mean creating objects dynamically, rather than classes, which
would be a different matter. If so, this poor terminology has resulted
in a lot of confusion in this thread, and you should ignore Oliver's
reply because he took your question at face value.)

Looks like you just need to look into the collections API -- HashMap,
ArrayList, etc. -- rather than trying to solve this with the contortions
you are looking at.

> > the code below doesnt
> > work once never mind for numerous classes?????
>
> > String help= TextField.getText();
> > help = new SOP();
>
> Is it possible to somehow extract the value of help above to create the
> class rather than using the variable name???

Let me guess at what you mean, since your question is completely
nonsensical. Did you mean that you want the contents of help to be a
variable name, and you want to initialize the variable of this name with
a new object of the SOP class? If so, you definitely want the
collections API, not what you're actually asking about.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
 
grahamsyuk





PostPosted: 2006-3-18 0:38:00 Top

java-programmer >> creating classes dynamically Oliver, cheers, i think the penny is finally starting to drop, glenn

 
 
Roedy Green





PostPosted: 2006-3-18 2:49:00 Top

java-programmer >> creating classes dynamically On 17 Mar 2006 07:10:08 -0800, "grahamsyuk" <email***@***.com>
wrote, quoted or indirectly quoted someone who said :

>HI is it possible to create classes dynamically and if so how? What im
>trying to do is have a user enter names into a textbox and use these
>names to create instances of classes dynamically. the code below doesnt
>work once never mind for numerous classes?????
>
> String help= TextField.getText();
>
> help = new SOP();

see http://mindprod.com/jgloss/onthefly.html
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.