Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / March 2006

Tip: Looking for answers? Try searching our database.

creating classes dynamically

Thread view: 
grahamsyuk - 17 Mar 2006 16:10 GMT
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 - 17 Mar 2006 16:36 GMT
> 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 - 17 Mar 2006 16:49 GMT
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
Oliver Wong - 17 Mar 2006 17:06 GMT
> 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
[quoted text clipped - 7 lines]
> 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?????
[quoted text clipped - 10 lines]
> 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
grahamsyuk - 17 Mar 2006 17:37 GMT
Oliver, cheers, i think the penny is finally starting to drop, glenn
Chris Smith - 17 Mar 2006 17:14 GMT
> -> In essence my program is to allow a user to create multiple Standard
> Operating procedures for various recipes. The program starts off empty.
[quoted text clipped - 3 lines]
> 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?????
[quoted text clipped - 4 lines]
> 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.

Signature

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

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

Thomas Weidenfeller - 17 Mar 2006 17:03 GMT
> 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
[quoted text clipped - 4 lines]
>        
>     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

Signature

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/

Roedy Green - 17 Mar 2006 19:48 GMT
>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
[quoted text clipped - 4 lines]
>       
>    help = new SOP();

see http://mindprod.com/jgloss/onthefly.html
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.